有没有办法撤消 Mocha 对any_instance 的存根?

发布于 2024-08-30 11:26:34 字数 573 浏览 9 评论 0原文

在我的控制器规格中,我是否有效?对于一些路由测试,(基于 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 的存根。

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

Within my controller specs I am stubbing out valid? for some routing tests, (based on Ryan Bates nifty_scaffold) as follows :-

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

This is fine when I test the controllers in isolation. I also have the following in my model spec

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

Again this works fine when tested in isolation. The problem comes if I run spec for both models and controllers. The model test always fails as the valid? method has been stubbed out. Is there a way for me to remove the stubbing of any_instance when the controller test is torn down.

I have got around the problem by running the tests in reverse alphabetic sequence to ensure the model tests run before the controllers but I really don't like my tests being sequence dependant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-09-06 11:26:34

您需要手动配置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

You need to manually configure 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

Also, remember that Rspec provides its own methods to mock an object. Use the RSpec API or you won't be able to benefit from library abstraction.
http://rspec.info/documentation/mocks/message_expectations.html

樱娆 2024-09-06 11:26:34

我遇到了同样的问题,但没有使用Rspec,而是使用正常的Test::Unit,实际上是ActionController::TestCase

定义的期望在测试中保持活力。

有什么线索可以让我在测试之间重置我的期望吗?

  • Ruby:1.9.2
  • Rails:3.0.3
  • Mocha:0.9.10

更新:我已经用 unstub Mocha 方法解决了这个问题:http://mocha.rubyforge.org/classes/Mocha/ObjectMethods.html#M000009

I'm having the same issue but not using Rspec but normal Test::Unit, actually ActionController::TestCase.

The defined expectations are keeping alive among the tests.

Is there any clue how can I reset my expectations between tests?

  • Ruby: 1.9.2
  • Rails: 3.0.3
  • Mocha: 0.9.10

Updated: I have solved this with the unstub Mocha method: http://mocha.rubyforge.org/classes/Mocha/ObjectMethods.html#M000009

被翻牌 2024-09-06 11:26:34

你的spec_helper

Spec::Runner.configure do |config|
  config.mock_with :mocha
end

是否包含rspec应该在测试之间拆除模拟。

Does your spec_helper contain

Spec::Runner.configure do |config|
  config.mock_with :mocha
end

With that rspec should tear down mocks between tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文