2011-07-26 31 views
0

所以我想确保回调发生在设置上用户实例期望保存...我的测试是:在集成测试与摩卡和水豚

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" 

,我也得到:

1 )失败: 测试:发布应该发布帖子。 (后测) [测试/集成/ post_test.rb:72:在__bind_1311708168_640179' /test/test_helper.rb:37:in expectation_on” 测试/集成/ post_test.rb:70:在`__bind_1311708168_640179' ]:被满意 不满足期望 并非所有期望: - 预期准确一次,尚未调用:#.publish_post!(any_parameters) 满意: - 允许任意次数,尚未调用:Post(id:integer,title:string,method_of_exchange:string,you_tube_url:string,lat:decimal ,lng:decimal,bounty:整数,距离:整数,user_id:整数,考虑__similar_offers:布尔值,描述:text,created_at:datetime,updated_at:datetime,地址:字符串,城市:字符串,状态:字符串,zip:字符串,国家:字符串,县:字符串,category_id:整数,slug:字符串,状态:字符串,流行度:整数,delta:布尔值,share_count:整数,needs_reply:decimal,needs_offer:decimal,district:string).facets(any_parameters) - 允许任意次数,尚未调用:{} .for(any_parameters) - 预期从来没有,没有调用:#.publish_post(any_parameters)

然而,我的帖子模式确实!

class Post < ActiveRecord::Base 
    belongs_to :user 
    after_create :publish 

    def publish 
    user.publish_post! 
    end 
end 

和我的职位控制器创建行动确实授予该用户的帖子...

class PostsController < ApplicationController 
    def create 
    post = Post.new(params[:post]) 
    post.user = current_user 
    post.save 
    end 
end 

...

手动测试时,该功能正常工作..所以我不明白为什么这不工作时自动?

回答

2

我真的不会尝试在集成测试中使用模拟期望。这种测试属于Post类本身的低级单元测试。尝试将集成测试视为仅从外部查看系统,就像用户那样。回调有什么影响?你能检查一下这种效果的结果吗?