why does this devise rspec model test fail ?

30 views
Skip to first unread message

Roelof Wobben

unread,
Aug 10, 2014, 7:25:25 AM8/10/14
to rs...@googlegroups.com
Hello,

I did make a admin user with Devise with rails g devise Admin.
I see that I have name and password,

But when I test it with this :

describe "Admin" do 
  it { is_expected.to respond_to(:name) }
  it { is_expected.to respond_to(:password) }
  it { is_expected.not_to respond_to(:to_model) }
  it { is_expected.not_to respond_to(:compact, :flatten) }
end

I see this output :

Failures:                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                 
  1) admins should respond to #name                                                                                                                                                                                                                                              
     Failure/Error: it { is_expected.to respond_to(:name) }                                                                                                                                                                                                                      
       expected "admins" to respond to :name                                                                                                                                                                                                                                     
     # ./spec/models/admin_users_spec.rb:2:in `block (2 levels) in <top (required)>'                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                 
  2) admins should respond to #password                                                                                                                                                                                                                                          
     Failure/Error: it { is_expected.to respond_to(:password) }                                                                                                                                                                                                                  
       expected "admins" to respond to :password                                                                                                                                                                                                                                 
     # ./spec/models/admin_users_spec.rb:3:in `block (2 levels) in <top (required)>'                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                 
Finished in 0.00226 seconds (files took 0.16221 seconds to load)                                                                                                                                                                                                                 
4 examples, 2 failures                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                 
Failed examples:                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                 
rspec ./spec/models/admin_users_spec.rb:2 # admins should respond to #name                                                                                                                                                                                                       
rspec ./spec/models/admin_users_spec.rb:3 # admins should respond to #password    

Roelof

Message has been deleted

Roelof Wobben

unread,
Aug 10, 2014, 7:33:57 AM8/10/14
to rs...@googlegroups.com
Sorry, here the right output :

Failures:                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                 
  1) Admin should respond to #name                                                                                                                                                                                                                                               
     Failure/Error: it { is_expected.to respond_to(:name) }                                                                                                                                                                                                                      
       expected "Admin" to respond to :name                                                                                                                                                                                                                                      
     # ./spec/models/admin_users_spec.rb:2:in `block (2 levels) in <top (required)>'                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                 
  2) Admin should respond to #password                                                                                                                                                                                                                                           
     Failure/Error: it { is_expected.to respond_to(:password) }                                                                                                                                                                                                                  
       expected "Admin" to respond to :password                                                                                                                                                                                                                                  
     # ./spec/models/admin_users_spec.rb:3:in `block (2 levels) in <top (required)>'                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                 
Finished in 0.00213 seconds (files took 0.16881 seconds to load)                                                                                                                                                                                                                 
4 examples, 2 failures                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                 
Failed examples:                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                 
rspec ./spec/models/admin_users_spec.rb:2 # Admin should respond to #name                                                                                                                                                                                                        
rspec ./spec/models/admin_users_spec.rb:3 # Admin should respond to #password   


Op zondag 10 augustus 2014 13:25:25 UTC+2 schreef Roelof Wobben:

Xavier Shay

unread,
Aug 10, 2014, 12:25:11 PM8/10/14
to rs...@googlegroups.com
expected "Admin" to respond to :name
 
This is saying 'expected the string "Admin" to respond', which isn't correct - you want to be testing against an instance of Admin.
 
You need to either describe the class so the correct automatic subject is created:
 
describe Admin do
  # ...
end
 
or explicitly assign a subject:
 
describe "Admin" do
  subject { Admin.new }
  # ...
end
 
fwiw, I wouldn't typically test that an object responds to specific methods, they don't tend to be valuable tests. Consider testing specific behaviours that you are interested in - those methods should all be executed.
 
Cheers,
Xavier
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
 

Roelof Wobben

unread,
Aug 10, 2014, 1:10:54 PM8/10/14
to rs...@googlegroups.com

Thanks,

the problem was also that I forget to use reguruire 'test-helpers" .

This problem is solved.

Roelof

Op zondag 10 augustus 2014 18:25:11 UTC+2 schreef Xavier Shay:
Reply all
Reply to author
Forward
0 new messages