setting expectation on user instance in integration test with mocha and capybara
So I am trying to ensure that a callback happens on save... My test is:
user = create_user
login user
visit new_post_path
fill_in "post_title", :with => "my post"
user.expects(:publish_post!).once
click_button "post_submit"
and I get:
1) Failure:
test: Post should place a post. (PostTest)
[test/integration/post_test.rb:72:in __bind_1311708168_640179'
/test/test_helper.rb:37:in
expectation_on'
test/integration/post_test.rb:70:in `__bind_1311708168_640179']:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #.publish_post!(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: Post(id: integer, title: string, method_of_exchange: string, you_tube_url: string, lat: decimal, lng: decimal, bounty: integer, distance: integer, user_id: integer, consider_similar_offers: boolean, description: text, created_at: datetime, updated_at: datetime, address: string, city: string, state: string, zip: string, country: string, county: string, category_id: integer, slug: string, status: string, popularity: integer, delta: boolean, share_count: integer, needs_reply: decimal, needs_offer: decimal, district: string).facets(any_parameters)
- allowed any number of times, not yet invoked: {}.for(any_parameters)
- expected never, not yet invoked: #.publish_post!(any_parameters)
Yet my post model does:
class Post < ActiveRecord::Base
belongs_to :user
after_create :publish
def publish
user.publish_post!
end
end
and my posts controller's create action does indeed assign the user the the post...
class PostsController < ApplicationController
def create
post = Post.new(params[:post]开发者_StackOverflow社区)
post.user = current_user
post.save
end
end
...
The functionality works fine when testing manually.. So I don't get why this is not working when automated?
I really wouldn't try to use mocking expectations in an integration test. That kind of testing belongs in low-level unit tests for the Post class itself. Try to think about integration tests as just looking at the system from the outside, as a user would. What effect does the callback have? Can you just check for the outcome of that effect?
精彩评论