在与 mocha 和 capybara 的集成测试中设置用户实例的期望
所以我试图确保在保存时发生回调...我的测试是:
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:整数,标题:字符串,method_of_exchange:字符串,you_tube_url:字符串,lat:小数,lng:小数,赏金:整数,距离:整数,user_id:整数,考虑相似的报价:布尔值,描述:文本,created_at:日期时间,updated_at:日期时间,地址:字符串,城市:字符串,州:字符串,邮政编码:字符串,国家/地区:字符串,县:字符串,category_id:整数,slug:字符串,状态:字符串,流行度:整数,增量:布尔值,share_count:整数,needs_reply:十进制,needs_offer:十进制,district:字符串).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
...
该功能在手动测试时工作正常..所以我不明白为什么这在自动化时不起作用?
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'
expectation_on'
/test/test_helper.rb:37:in
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])
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的不会尝试在集成测试中使用模拟期望。这种测试属于 Post 类本身的低级单元测试。尝试将集成测试视为只是从外部查看系统,就像用户一样。回调有什么作用?你能检查一下该效果的结果吗?
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?