Ruby on rails 在与mocha和capybara的集成测试中设置对用户实例的期望
所以我试图确保在保存时发生回调。。。我的测试是:Ruby on rails 在与mocha和capybara的集成测试中设置对用户实例的期望,ruby-on-rails,capybara,mocha.js,Ruby On Rails,Capybara,Mocha.js,所以我试图确保在保存时发生回调。。。我的测试是: 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) 失败: 测试:帖子应该放置一个帖子。(测试后) [test/integration/post_test.rb:72:i
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) 失败:
测试:帖子应该放置一个帖子。(测试后)
[test/integration/post_test.rb:72:in\u绑定\u 1311708168\u 640179'
/test/test\u helper.rb:37:in
expection\u on'
test/integration/post_test.rb:70:in`__bind_1311708168_640179']:
并非所有的期望都得到了满足
未满足的期望:
-只需一次,尚未调用:#.发布(post)!(任何_参数)
满意的期望:
-允许任意次数,尚未调用:Post(id:integer,title:string,method\u of_exchange:string,you\u tube\u url:string,lat:decimal,lng:decimal,bounty:integer,distance:integer,user\u id:integer,consumeric\u相似的\u提供:boolean,description:text,created\u at:datetime,updated\u:datetime,address:string,city:string,state:string,zip:string,country:string,country:string,country:string,类别\u id:integer,slug:string,status:string,popularity:integer,delta:boolean,share\u count:integer,needs\u reply:decimal,needs\u offer:decimal,district:string)。facets(任何\u参数)
-允许任意次数,尚未调用:{}.for(任意_参数)
-预期从不,尚未调用:#.publish_post!(任何_参数)
然而,我的post模型做到了:
class Post < ActiveRecord::Base
belongs_to :user
after_create :publish
def publish
user.publish_post!
end
end
class Post
我的帖子控制器的创建操作确实会将帖子分配给用户
class PostsController < ApplicationController
def create
post = Post.new(params[:post])
post.user = current_user
post.save
end
end
class PostsController
手动测试时,功能运行良好。因此,我不明白为什么这在自动化测试时不起作用?我真的不会尝试在集成测试中使用模拟预期。这种测试属于后期类本身的低级单元测试。试着将集成测试视为从就像用户一样。回调有什么影响?你能检查一下这种影响的结果吗