【问题标题】:setting expectation on user instance in integration test with mocha and capybara在与 mocha 和 capybara 的集成测试中设置对用户实例的期望
【发布时间】:2011-07-26 19:38:38
【问题描述】:

所以我试图确保在保存时发生回调...我的测试是:

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:in __bind_1311708168_640179' /test/test_helper.rb:37:inexpectation_on' 测试/集成/post_test.rb:70:in `__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: 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 ,流行度:整数,delta:布尔值,share_count:整数,needs_reply:十进制,needs_offer:十进制,地区:字符串).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

...

该功能在手动测试时工作正常。所以我不明白为什么在自动化时这不起作用?

【问题讨论】:

    标签: ruby-on-rails capybara mocha.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      相关资源
      最近更新 更多