RSpec giving undefined method `save' for #<RSpec::Mocks::AllowanceTarget:0x9bfadd4> error

64 views
Skip to first unread message

Joy Ganguly

unread,
Jul 28, 2014, 10:16:43 AM7/28/14
to rs...@googlegroups.com
Running Rspec giving error

undefined method `save' for #<RSpec::Mocks::AllowanceTarget:0x9bfadd4>

and here is my code and error details https://gist.github.com/himadriganguly/e1547a512ff54084932d

Thanks to all in advance.

Aaron Kromer

unread,
Jul 28, 2014, 6:09:50 PM7/28/14
to rs...@googlegroups.com

It looks like line 27 should be double(Reader) instead of allow(Reader).

allow returns a mock expectation, not the object to mock. Additionally, line 37 will overwrite the stub you created on line 36; meaning that it will return nil. Add the .and_return(reader) to line 37 and delete line 36.

Hope that helps.



--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/3b2a5bb2-0ef5-4843-9589-0f6d995958d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joy Ganguly

unread,
Jul 31, 2014, 12:20:06 PM7/31/14
to rs...@googlegroups.com
Hi Aaron!

Thanks a lot for your reply.

I have change the code accordingly as you have said but now i am getting this error
Failure/Error: post :create, reader: params
Double Reader(id: integer, created_at: datetime, updated_at: datetime, email: string) received unexpected message :save
with (no args)

The code and error is updated at https://gist.github.com/himadriganguly/e1547a512ff54084932d

Thank you and take care.

Aaron Kromer

unread,
Jul 31, 2014, 1:18:03 PM7/31/14
to rs...@googlegroups.com

double objects only respond to messages you tell it to. Since none have been defined, when save is sent to it, it doesn’t know how to respond and it fails with that message. If you want to respond to all messages by default use .as_null_object which will always return nil. Otherwise provide stub return values for those messages you care about.

Just an FYI, but when rspec-rails 3.0 was released, the controller specs were changed to no longer try to use stubs by default. The reasoning for this is that default controllers are very tightly coupled to the ActiveRecord::Base implementations. While you are subclassing this, the majority of the API (which has a large surface area) is not controller by you. There is definitely some debate about stubbing code you own vs code you don’t own. The core team decided to go the route of not stubbing controllers due to this very tight coupling.

We’d encourage you to explore a more domain specific API which you control if you wish to stub controllers.



--
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.

Joy Ganguly

unread,
Aug 1, 2014, 2:37:56 AM8/1/14
to rs...@googlegroups.com
Hi!

Thanks a lot for you help. I have corrected the errors can you please have a look at the code and verify is the code correct or not.

And also i am stuck with another error.

Please have a look here https://gist.github.com/himadriganguly/e1547a512ff54084932d

Thanks and take care.


On Monday, 28 July 2014 19:46:43 UTC+5:30, Joy Ganguly wrote:
Reply all
Reply to author
Forward
0 new messages