Hey, I'm using TDD with rails for the first time... interesting concepts. Definitely useful. That is, until I come to this. When I run my test I get:
1) User should build the full name correctly
Failure/Error: #u1.fullname.to_s.should be("#{#attr[:firstname]} #{#attr[:lastname]}")
expected Joe Smith, got "Joe Smith"
# ./spec/models/user_spec.rb:35:in `block (2 levels) in <top (required)>'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/rspec-core-2.0.0.beta.18/lib/rspec/monkey/spork/test_framework/rspec.rb:4:in `run_tests'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:13:in `block in run'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:21:in `block in initialize'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `fork'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `initialize'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `new'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `run'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/server.rb:47:in `run'
where the test is:
it 'should build the full name correctly' do
#u1.fullname.should be("#{#attr[:firstname]} #{#attr[:lastname]}")
end
and the supporting code is:
def fullname
"#{firstname} #{lastname}"
end
So obviously this works, but what's with the quotation marks? Have I missed something head-smackingly obvious?
Your problem is coming from the fact that you are using be instead of eql. be is expecting a class in the way you've set it up (documentation). Try writing your spec as
#u1.fullname.should eql("#{#attr[:firstname]} #{#attr[:lastname]}")
Documentation for eql
Also, note the difference between eql and the method directly below it in the documentation, equal.
Related
I am trying to test my application and I continually get the following error:
Error:
ContractsControllerTest#test_should_get_show:
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
Error:
ContractsControllerTest#test_should_get_show:
NoMethodError: undefined method `each' for nil:NilClass
Below is the code on my contract controller and I have similar code like this on all the controllers.
def index
#contract = Contract.all.paginate(page: params[:page], :per_page => 70)
end
def show
#contract = Contract.find(params[:id])
end
def new
#contract = Contract.new
end
def create
#contract = Contract.new(located)
if #contract.save
flash[:success] = "A record has been successfully added"
redirect_to contracts_path
else
render 'new'
end
end
def located
params.require(:contract).permit(:contract_name, :contract_status, :services_rendered, :contract_value, :award_year)
end
# editing a record in the contract from cotract table
def edit
#contract = Contract.find(params[:id])
end
def update
#contract = Contract.find(params[:id])
if #contract.update_attributes(located)
flash[:success] = "Contract form updated"
redirect_to contracts_path
else
render'edit'
end
end
def destroy
Contract.find(params[:id]).destroy
flash[:success] = "A record has been successfully deleted"
redirect_to contracts_path
end
Below is the full trace of the error when I run the command rails db:fixtures:load --trace
rails aborted!
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:72:in `validate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:49:in `raw_rows'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:37:in `config_row'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:27:in `model_class'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:791:in `block (2 levels) in
read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:15:in `open'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:790:in `block in read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `each_with_object'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:598:in `initialize'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:529:in `new'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:529:in `block (2 levels) in
create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:526:in `map'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:526:in `block in create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/connection_adapters/postgresql/referential_integrity.rb: 22:in `disable_referential_integrity'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:523:in `create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/railties/databases.rake:207:in `block (3 levels) in
<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands.rb:18:in `<top (required)>'
/home/ubuntu/workspace/final_project/bin/rails:9:in `require'
/home/ubuntu/workspace/final_project/bin/rails:9:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/rails.rb:28:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/rails.rb:28:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/command.rb:7:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in
`run'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top
(required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/binstub.rb:31:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/binstub.rb:31:in`<top (required)>'
/home/ubuntu/workspace/final_project/bin/spring:15:in `require'
/home/ubuntu/workspace/final_project/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:fixtures:load
(See full trace by running task with --trace)
My application containts 12 controllers, the same error occurs in each of the controllers. Also, I have 10 tables in my schema some of which contain references / relations to each other.
I don't really know what is wrong and I have be struggling with this problem for about four days now. I would be grateful if anyone could help me with any code or information that could help me to trace the source of the error. I would also be grateful if you could provide information for exactly where I should place the code to trace and solve this error.
Thanks
One of your fixture files in test/fixtures has bad formatting. The problem does not appear to be with your controllers or tests since it is happening for every single one of them. All of the fixtures get loaded into the test database before every single test, even if some are not used for the current test. So if there is an error in one of these files it will get raised for every test.
According to the line from the activerecord source that is throwing the error at the top of the stack trace you've provided, ActiveRecord is expecting every entry in your YAML fixture file to be a key which references a hash.
Each entry in a fixture should represent a model instance. The top level key is used as a name to reference that instance (rails usually names them one and two when generating the fixture template for a new model). Here is an example YAML fixture with one good entry and a few different types of bad ones.
# This will produce a hash associated to key :hash_entry.
# This is the correct type of entry, all others that follow are incorrect for rails fixtures.
hash_entry:
key1: value1
key2: value2
# This will produce the string "not a hash" associated to key :string_entry
string_entry: not a hash
# This will produce the array ["also", "not","a","hash"] associated to key :array_entry
array_entry:
- also
- not
- a
- hash
# This will produce nil associated to key :nil_entry
nil_entry:
You need to examine your fixtures files in test/fixtures and look for any that have the bad formatting as described above. Here is a rake task that will help you identify which files and entries need to be corrected. First run rails g task fixtures check_format, and place this code inside of the rake file that gets generated at lib/tasks/fixtures.rake.
namespace :fixtures do
desc "Looks for bad fixture files"
task check_format: :environment do
fixtures_dir = Rails.root.join("test", "fixtures")
fixture_files = Pathname.glob("#{fixtures_dir}/**/*.yml")
fixture_files.each do |file|
fixture = YAML.load(IO.read(file))
fixture.each_pair do |name, entry|
puts "Bad fixture entry #{name}: #{entry.inspect} in fixture #{file}" unless entry.is_a? Hash
end
end
end
end
Then run rails fixtures:check_format and the offending files and entries will be printed out on the command line for you to find and correct.
I am a beginner with Rspec, and I found out my terminal output like this.
It's really in a mess and hard to understand test result.
Unlike the output in official tutorial.
Should I install some tools or modify some configuration?
Update
zombie.rb
class Zombie
attr_accessor :name
def initialize
#name = 'Error_Ash'
end
end
zombie_spec.rb
require "spec_helper"
require "zombie"
#give Class
describe Zombie do
# example
it "is named Class_Ash"
zombie = Zombie.new
zombie.name.should == "Ash"
end
error msg
Coda:rspec_pra Coda$ rspec spec/lib/zombie_spec.rb --format doc
/Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:87:in `block in <module:Support>': expected: "Ash" (RSpec::Expectations::ExpectationNotMetError)
got: "Error_Ash" (using ==)
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:96:in `call'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:96:in `notify_failure'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:71:in `fail_with_message'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:106:in `__delegate_operator'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:91:in `eval_match'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:51:in `block in use_custom_matcher_or_delegate'
from /Users/Coda/Desktop/code/ruby_pra/rspec_pra/spec/lib/zombie_spec.rb:11:in `block in <top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `module_exec'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `subclass'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/dsl.rb:82:in `block (2 levels) in expose_example_group_alias_globally'
from /Users/Coda/Desktop/code/ruby_pra/rspec_pra/spec/lib/zombie_spec.rb:6:in `<top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/bin/rspec:22:in `load'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/bin/rspec:22:in `<main>'
Coda:rspec_pra Coda$
If there is nothing in your spec_helper that somehow overrides the --format switch (which I do not even know if it's possible) then this should give you readable tests that pass:
zombie.rb exactly as you posted,
zombie_spec
require "spec_helper"
require "zombie"
describe Zombie do
it "is named Ash" do
zombie = Zombie.new
expect(zombie.name).to eq "Ash"
end
end
and command rspec spec/lib/zombie_spec.rb -f d which is short for --format documentation
EDIT*: Oh sorry, the Error you posted is indeed an RSpec error... Its just not formated, as you said x.x
try the --tty flag maybe?
rspec spec/lib/zombie_spec.rb --tty -f d
I have a study-case project, working with Rails 4, RSpec, FactoryGirl, Faker...
But rspec tests are failing randomly, on development env. I'am searching a lot, but if anyone could help me figure it out, I would appreciated.
Here are the links to the test file, factory and the model:
Model to be tested: https://github.com/freaktags/core/blob/master/app/models/user.rb
User Factory: https://github.com/freaktags/core/blob/master/spec/factories/users.rb
Test itself: https://github.com/freaktags/core/blob/master/spec/models/user_spec.rb
When it fails, produce always a stack almost like this:
Failure/Error: #chloe = FactoryGirl.create(:user)
ActiveRecord::RecordInvalid:
translation missing: pt-BR.activerecord.errors.messages.record_invalid
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/validations.rb:57:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `block in save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:208:in `transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activesupport-4.1.8/lib/active_support/notifications.rb:161:in `instrument'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
You changed the locale to something other than the rails default and are getting errors that don't have a translation into that locale.
An easy work-around would be to temporarily change the locale back to the default so you are more likely to get the actual error message (Looks like activerecord is trying to tell you something but it doesn't know how to say it in pt-BR). It will tell you this error in English, but it seems like you know English so that shouldn't be a problem.
Fix the error, then switch the locale back to your desired locale and the error shouldn't appear any more.
If you need to avoid changing the locale you will need to add a translation appropriate to your chosen locale for the specific error. There are sources elsewhere that can help with this process. Here are some links with related information:
http://guides.rubyonrails.org/i18n.html
http://archive.railsforum.com/viewtopic.php?id=46725
ActiveRecord error messages: translation for fields
Update:
A shot in the dark, but your specific error may have to do specifically with FactoryGirl having issues during initialization. . . This answer looks like it may work with your problem: https://stackoverflow.com/a/11644746/1026898.
The problems was Faker, it doesn't provide a control for email and username values. Anyway Ecnalyr helped a lot on finding the problem :)
This seemed to be a flickering sort of a bug for a while,but now it's appearing consistently: when I run RSpec on a fairly simple ApplicationHelper spec, I get the following error:
% rspec --backtrace
1) ApplicationHelper renders Markdown from plain text
Failure/Error: expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
NameError:
undefined local variable or method `helper' for #<RSpec::ExampleGroups::ApplicationHelper_2:0x000001248d1218>
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-expectations-0f7b78587ab4/lib/rspec/matchers.rb:903:in `method_missing'
# ./spec/helpers/application_helper_spec.rb:4:in `block (2 levels) in <top (required)>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `block in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-rails-480b173c9ad6/lib/rspec/rails/adapters.rb:67:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:432:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:485:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:301:in `with_around_example_hooks'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:145:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:457:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/reporter.rb:49:in `report'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:86:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:70:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/exe/rspec:4:in `<top (required)>'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `load'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `<main>'
Here's the complete spec file (spec_helper is included as part of my .rspec file):
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
This was working up until recently, but I'm not certain what I could have done to break such a basic feature. I'm using edge versions of Rails and RSpec, but didn't see anything in their git repos to suggest that helper had been deprecated---and running rails g helper foo still generates a foo_helper_spec.rb file with instructions indicating that helper contains the helper itself. If anyone has any ideas, I'd be grateful for them!
I created a new Rails project with a fresh RSpec installation, and it led me to the problem. Apparently one of the recent betas introduced a configuration directive called config.infer_spec_type_from_file_location! that was missing from my slightly older spec_helper file; without it, RSpec wasn't guessing the spec type and mixing in the associated methods. Beware breaking changes!
add :type => :helper
so your code should look like
describe ApplicationHelper, type: :helper do
...
end
That's a weird error !! are you sure you required spec_helper in your spec ?
Anyway you could try without the helper method:
First you should first add to /spec/spec_helper.rb the following:
RSpec.configure do |config|
...
config.include ApplicationHelper
end
Then test without helper , so it will be:
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
From the backtrace, it doesn't look like you're using the rspec-rails gem - just rspec-core and rspec-expectations.
rspec-rails is what provides the helper method to your helper specs. From inside a spec in my codebase:
(rdb:1) self.method(:helper).source_location
["/home/becky/.gem/ruby/2.0.0/gems/rspec-rails-2.14.1/lib/rspec/rails/example/helper_example_group.rb", 19]
I am trying an rspec test where the example looks like so:
it "cannot be deleted if it has children" do
children = generate_children
children.each do |child|
child.parent = #subj
#subj.save
child.save
#subj.destroy.should be_false
#subj.should have(1).error
end
end
I get this in the backtrace:
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:21:in `handle_matcher'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:27:in `should'
./spec/models/my_examples.rb:14
./spec/models/my_examples.rb:10:in `each'
./spec/models/my_examples.rb:10
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:49:in `instance_eval'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:49:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `with_pending_capture'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `run_examples'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'
But the test passes, although with the ugly backtrace shown above.
Any idea how I could rewrite this test / am I doing something wrong here?
line14 is the destroy-line - which is interesting.
According to the source code, destroy doesn't return true/false - it returns the same item (through freeze).
I don't think you're expected to check if it was "true". It's probably failing because an frozen Active Record isn't false... but it's kind of a weird thing to compare with.
Not sure how you'd go about checking it...perhaps with the equivalent of
lambda do #subj.destroy end.should_not change(Subject, :count).by(-1)