On Dec 1, 2011, at 6:34 PM, Patrick J. Collins wrote:
> I've got something like this:
>
> # post_observer.rb
>
> after_create
> # ...stuff
> Delayed::Job.enqueue(PostSharer.new(post, post.user))
> end
>
> ...
>
> # post_sharer.rb
>
> class PostSharer < Struct.new(:post, user)
>
> def perform
> # Delayed::Job calls .perform on the object passed into enqueue
> end
>
> end
>
> # post_controller_spec.rb
>
> it "shares the post" do
>
> PostSharer.expects(:new).once
> lambda { do_post }.should change(Delayed::Job, :count).by(1)
>
> end
>
> ...
>
> This fails due to the expectation put on PostSharer receiving .new --- if I
> remove that, then it all works fine... And if I look at the test database,
> Delayed::Job has created a job for PostSharer, so it is all working as
> desired..
You could automate "if I look at the test database" bit as part of the test.
> I just wanted to take it a step further and ensure that the right
> class is being instantiated.
Wouldn't the record in the database be enough? If not, you could do this:
Delayed::Job.expects(:enqueue).with(PostSharer.new(post, post.user))
> I am assuming this is because setting an
> expectation on new is somehow changing the structure of the class and
> confusing
> delayed job? Maybe because it's a struct?
In rspec-mocks, when you say Klass.stub(:new), Klass.new returns a proxy, not
an instance of the Klass. I'm pretty sure the same is true of mocha.
Cheers,
David
> 1) PostsController creating a post sharing shares when it should
> Failure/Error: post :create, { :submit_action => submit_type.to_s, :post
> => new_post(post_attributes).attributes }
> ArgumentError:
> Cannot enqueue items which do not respond to perform
> # ./app/observers/post_observer.rb:12:in `after_create'
> # ./app/models/post.rb:156:in `set_state_to_open_for_free_requests'
> # ./app/controllers/posts_controller.rb:39:in `create'
> # ./spec/controllers/post_controller_spec.rb:8:in `do_post'
> # ./spec/controllers/post_controller_spec.rb:77
> # ./spec/controllers/post_controller_spec.rb:75
>
> Patrick J. Collins
> http://collinatorstudios.com
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users