Here is the code
#add email of team lead of the current user
def add_the_tl_email(to_address, session_user_id)
ul = UserLevel.find_by_user_id(session_user_id)
if !ul.team.nil? && !ul.role.nil?
User.user_status('active').joins(:user_levels).where(:user_levels => {:position => 'tl',
:role => ul.role,
:team => ul.team }).each do
|u| to_address << u.email
end
end
end
The error from spec:
1) CustomersController GET customer page 'create' successful should create one customer record
Failure/Error: post 'create', :customer => #customer
NoMethodError:
undefined method `team' for nil:NilClass
# ./app/mailers/user_mailer.rb:36:in `add_the_tl_email'
# ./app/mailers/user_mailer.rb:15:in `notify_tl_dh_ch_ceo'
# ./app/controllers/customers_controller.rb:27:in `create'
# ./spec/controllers/customers_controller_spec.rb:48:in `block (5 levels) in <top (required)>'
# ./spec/controllers/customers_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
team is a field in user_level. Here is the user_level in factory:
Factory.define :user_level do |level|
level.role "sales"
level.position "member"
level.team 1
level.association :user
end
In rails console, the ul.team.nil? returns either true or false on test data generated by factory.
Since team is a field in user_level, why there is error in spec like: NoMethodError:
undefined method `team' for nil:NilClass ?
Thanks.
The issues is that nil.team is not a method*
NoMethodError:
undefined method `team' for nil:NilClass
That is, ul evaluates to nil: check ul.nil? before checking ul.team.nil?... but might want to find out why the find failing to start with ;-)
Happy coding.
*This is what the real error message says, the title of the post is garbage :-/
The call in
ul = UserLevel.find_by_user_id(session_user_id)
returned nil and thus nil was assigned to ul.
So when you called
!ul.team.nil?
Ruby tried to call team on nil. nil is an instance of NilClass and there is no method named team on NilClass.
Related
I have these models:
Class A
embeds_many :b
end
Class B
belongs_to :c
end
Class C
end
I'm working with rails_admin and mongoid. In admin, when I try to retrieve the list of C records when I'm creating an A instance I'm getting this error:
This only happens on production envirnment not in development
NoMethodError (undefined method `associations' for nil:NilClass):
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid/abstract_object.rb:10:in `initialize'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `new'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `get'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/app/controllers/rails_admin/main_controller.rb:138:in `get_association_scope_from_params'
Taking a look at rails_admin code we can see that piece of code in mongoid.rb file.
def get(id)
AbstractObject.new(model.find(id))
rescue => e
raise e if %w(
Mongoid::Errors::DocumentNotFound
Mongoid::Errors::InvalidFind
Moped::Errors::InvalidObjectId
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end
If we pay attention at this code we can see that model.find(id) must produce a Mongoid::Errors::DocumentNotFound if document doesn't exists by default.
However, in mongoid you can avoid the raise of this error with raise_not_found_error: true in mongo config file, this produce the undefined method of nil class.
Tracking issue on github
I have a simple model with sequence as a attribute as integer. When I am setting sequence as 1 it fail the simple test case ie:
context 'MathFactAttemptData' do
it 'should insert rows' do
math_fact_attempt_data = FactoryGirl.build :math_fact_attempt_data
#params[:request_params] = {
user_data: {
math_fact_attempt_data: [JSON.parse(math_fact_attempt_data.to_json)]
}
}
initial_math_fact_attempt_data_count = MathFactAttemptData.unscoped.count
post api_v3_user_data_path, #params
response.should be_success
response_body = JSON.parse response.body
response_body['user_data'].should be_nil
response_body['seed_data'].should be_nil
MathFactAttemptData.unscoped.count.should == initial_math_fact_attempt_data_count + 1
end
end
Factories:
factory :math_fact_attempt_data do
association :user, factory: :student
association :math_fact_attempt, factory: :math_fact_attempt
association :problem_type, factory: :problem_type
num1 1
num2 1
correct_answer 1
response 1
correct 1
#sequence 1
time_spent 1
choice_type "MyString"
selected_operator "MyString"
end
Uncommenting sequence fails the test case with issue ie:
API v3 POST /user_data.json Entities MathFactAttemptData should insert rows
Failure/Error: math_fact_attempt_data = FactoryGirl.build :math_fact_attempt_data
NoMethodError:
undefined method `to_sym' for 1:Fixnum
# ./spec/requests/api/v3/post_user_data_spec.rb:1116:in `block (5 levels) in <top (required)>'
Finished in 3.7 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/api/v3/post_user_data_spec.rb:1115 # API v3 POST /user_data.json Entities MathFactAttemptData should insert rows
As Peter pointed out, sequence is a FactoryGirl method.
Try this for setting the sequence attribute:
FactoryGirl.define do
factory :math_fact_attempt_data do
add_attribute :sequence, 1
end
end
sequence is a FactoryGirl method which expects a symbol as it's first parameter. That's why you're getting the error you're getting.
I am setting up a test that should expect calls on two "subscriber" instances:
it "sends out sms to all the subscribers" do
#user.subscribers.create!
#user.subscribers.create!
Subscriber.any_instance.should_receive(:send_message).with(#message).times(2)
post :create, {:broadcast => valid_attributes}
end
The actual code is:
def create
#broadcast = Broadcast.new(params[:broadcast])
current_user.subscribers.each do |subscriber|
subscriber.send_message(#broadcast.message)
end
...
The error:
Failure/Error: post :create, {:broadcast => valid_attributes}
ArgumentError:
wrong number of arguments (1 for 0)
# ./app/controllers/broadcasts_controller.rb:41:in `block in create'
# ./app/controllers/broadcasts_controller.rb:40:in `create'
# ./spec/controllers/broadcasts_controller_spec.rb:73:in `block (4 levels) in <top (required)>'
For some reason, if I add the line: Subscriber.any_instance.should_receive(:send_message).with(#message).times(2), it fails with that error message. If I remove that line, the test runs smoothly (no wrong number of argument problem). What am I doing wrong?
The error you're getting is because the 'times' method is expected to be chained onto one of the other "receive count" expectations. You can use any one of the following:
should_receive(:send_message).with(#message).exactly(2).times
should_receive(:send_message).with(#message).at_most(2).times
You can also use one of the other alternatives that does not require the 'times' method:
should_receive(:send_message).with(#message).twice
should_receive(:send_message).with(#message).at_most(:twice)
More on that can be found in the rspec-mocks documentation
You may need to set the expectation BEFORE creating the subscribers:
it "sends out sms to all the subscribers" do
Subscriber.any_instance.should_receive(:send_message).with(#message).twice
#user.subscribers.create!
#user.subscribers.create!
post :create, {:broadcast => valid_attributes}
end
I'm getting these failures when running rspec spec/. The spec that is failing is was auto-generated with the scaffolding. I'm trying to understand RSpec but I don't know where to begin looking for the cause other than it feels like some method is missing?!? Yet, app appears to be working fine. Nothing about these failures appears in test.log. Is there another place I should be looking for hints to track this down?
$ rspec spec/
.....................F.F.
Failures:
1) clowns/edit.html.haml renders the edit clown form
Failure/Error: render
undefined method `to_sym' for nil:NilClass
# ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/edit.html.haml:3:in `_app_views_clowns_edit_html_haml___574620942879655923_2176081980_599706797287605391'
# ./spec/views/clowns/edit.html.haml_spec.rb:13:in `block (2 levels) in <top (required)>'
2) clowns/new.html.haml renders new clown form
Failure/Error: render
undefined method `to_sym' for nil:NilClass
# ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/new.html.haml:3:in `_app_views_clowns_new_html_haml__1085372210838170129_2159651900_599706797287605391'
# ./spec/views/clowns/new.html.haml_spec.rb:12:in `block (2 levels) in <top (required)>'
Finished in 1.03 seconds
27 examples, 2 failures, 2 pending
And here is the spec that apparently fails (edit.html.haml_spec.rb). It was auto-generated by rails g scaffold Clown name:string balloons:integer:
#spec/views/clowns/edit.html.haml_spec.rb
require 'spec_helper'
describe "clowns/edit.html.haml" do
before(:each) do
#clown = assign(:clown, stub_model(Clown,
:name => "MyString",
:balloons => 1
))
end
it "renders the edit clown form" do
render
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
assert_select "form", :action => clown_path(#clown), :method => "post" do
assert_select "input#clown_name", :name => "clown[name]"
assert_select "input#clown_balloons", :name => "clown[balloons]"
end
end
end
Are you also using simple_form (or perhaps even formtastic)? I get the same error when I convert the scaffold forms from standards forms to simple_form forms (form_for(#user) => simple_form_for(#user)).
It still works, as you say, so I'm not sure it's worth worrying about. I think you'd be better off just letting Cucumber worry about it. Sarah Mei says view specs are a waste of time. :-)
I just started to use factory girl to replace fixtures when I am testing. I am working on a twitter client and I am trying to use factory girl to create the twitter objects for testing. When I create them individually it is fine. But, if I try to associate them I get the error below.
Factory.define :status, :class => Twitter::Status, :default_strategy => :build do |t|
t.text 'Test Twitter Status message'
t.association :user, :factory => :twitter_user #this line causes the problems
end
Factory.define :twitter_user, :class => Twitter::User, :default_strategy => :stub do |u|
u.profile_image_url "#{RAILS_ROOT}/public/images/rails.png"
end
The t.association :user, :factory => :twitter_user causes the problems because when it is there this exception is thrown. Is there anyway to fix this? Or is factory girl just designed for activerecord objects? Thanks
NoMethodError: undefined method `save!' for #<Twitter::User:0x4af3de46>
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/create.rb:5:in `result'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:293:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:237:in `create'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/build.rb:17:in `associate'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/attribute/association.rb:13:in `add_to'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:290:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `each'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:217:in `build'
test/functional/tweet_feeds_controller_test.rb:12:in `test_Display_friends_timeline_for_the_'amber'_user'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run_with_callbacks_and_mocha'
I'm pretty sure that factory girl is just for ActiveRecord objects, you should just be able to mock up and stub a Twitter::User object rather than use a Factory, if you were using rSpec maybe (if my syntax is right):
#twitter_user = mock(Twitter::User, :profile_image =>"#{RAILS_ROOT}/public/images/rails.png")
and then attach that to your object that needs it.
This might not be quite right, but it's the path I would start down.