加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 有没有办法撤消any_instance的Mocha存根?

发布时间:2020-12-17 02:50:55 所属栏目:百科 来源:网络整理
导读:在我的控制器规格中,我是有效的?对于一些路由测试,(基于Ryan Bates nifty_scaffold)如下: – it "create action should render new template when model is invalid" do Company.any_instance.stubs(:valid?).returns(false) post :create response.should
在我的控制器规格中,我是有效的?对于一些路由测试,(基于Ryan Bates nifty_scaffold)如下: –

it "create action should render new template when model is invalid" do
  Company.any_instance.stubs(:valid?).returns(false)
  post :create
  response.should render_template(:new)
end

当我单独测试控制器时,这很好.我的模型规格中也有以下内容

it "is valid with valid attributes" do
  @company.should be_valid
end

在隔离测试时,这种方法再次正常工作.如果我为模型和控制器运行规范,问题就来了.模型测试总是失败为有效?方法已被删除.当控制器测试被拆除时,有没有办法删除any_instance的存根.

我通过以反向字母顺序运行测试来解决问题,以确保模型测试在控制器之前运行,但我真的不喜欢我的测试依赖于序列.

解决方法

您需要手动配置RSpec.

Rspec.configure do |config|
  ...

  # == Mock Framework
  #
  # If you prefer to use mocha,flexmock or RR,uncomment the appropriate line:
  #
  config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  # config.mock_with :rspec
end

另外,请记住,Rspec提供了自己的方法来模拟对象.使用RSpec API或者您将无法从库抽象中受益.
http://rspec.info/documentation/mocks/message_expectations.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读