【问题标题】:Trouble mocking on cucumber + rails麻烦嘲笑黄瓜+铁轨
【发布时间】:2010-03-19 20:03:16
【问题描述】:

我在尝试为黄瓜上的导轨模型定义模拟时遇到了很多麻烦。似乎该方法正在创建一堆消息期望,并且我不断收到以下错误:

 Given I have only a product named "Sushi de Pato" # features/step_definitions/product_
steps.rb:19
      unexpected invocation: #<Mock:ProductCategory_1001>.__mock_proxy()
      unsatisfied expectations:
      - expected exactly once, not yet invoked: #<Mock:ProductCategory_1001>.errors(any_pa
rameters)
      - expected exactly once, not yet invoked: #<Mock:ProductCategory_1001>.id(any_parame
ters)
      - expected exactly once, not yet invoked: #<Mock:ProductCategory_1001>.to_param(any_
parameters)
      - expected exactly once, not yet invoked: #<Mock:ProductCategory_1001>.new_record?(a
ny_parameters)
      - expected exactly once, not yet invoked: #<Mock:ProductCategory_1001>.destroyed?(an
y_parameters)
      satisfied expectations:
      - allowed any number of times, not yet invoked: #<Mock:errors>.count(any_parameters)

       (Mocha::ExpectationError)

我还没有实现 ProductCategory 类,我只是希望它返回一个 ID 和一个“名称”属性。

这是我的步骤定义:

Given /^I have only a product named "([^\"]*)"$/ do |name|
  @product = Product.create!(:name => name, :description => 'Foo', :price => 100, :points => 100, :category => mock_model(ProductCategory))
end

这是我的 env.rb 文件:

$: << File.join(File.dirname(__FILE__),"..")

require 'spec\spec_helper

我正在使用 RSPec 1.3.0、黄瓜 0.6.3 和 webrat 0.7.0

我也尝试过使用存根,但出现了一些其他错误...

【问题讨论】:

    标签: ruby-on-rails mocking rspec cucumber bdd


    【解决方案1】:

    真的不建议在黄瓜中模拟模型。 Cucumber 旨在成为一个完整的堆栈、由外而内的测试框架。

    如果您尚未实现 ProductCategory 类,我建议您从 Product 中删除类别关联,然后只测试您已实现的功能。

    当您开始实施 ProductCategory 时,您可以试驾它。

    【讨论】:

    • 我认为你是绝对正确的!在网上阅读了很多关于黄瓜试图解决这个问题的东西之后,我得出了这样一个结论,即黄瓜应该更多地作为一个集成测试,因此需要我描述的每一个东西都被实现。我通过创建 ProductCategory 模型规范然后实现它来解决它。我认为这是有道理的,因为 Product 和 ProductCategory 密切相关。非常感谢!
    【解决方案2】:

    就我而言,这是因为我激活了另一个模拟框架而我忘记了这一点。我通过再次注释而不是 spec/spec_helper.rb 中的行来修复它:

      # == Mock Framework
      #
      # RSpec uses its own mocking framework by default. 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
    

    这样 RSpec 将使用自己的模拟框架

    【讨论】:

      【解决方案3】:

      我认为 jrallison 回答了这个问题,但无论如何,如果您想在 cucumber 上模拟模型以实现外部连接或日期/时间操作等行为,您可以使用我从 cukes 的一位程序员同事那里得到的以下答案讨论列表:

      这是我在 env.rb 中所做的: 需要“规范/模拟”

      Before do
       # To get RSpec stubs and mocks working.
       $rspec_mocks ||= Spec::Mocks::Space.new
      end
      
      After do
         begin
           $rspec_mocks.verify_all
         ensure
           $rspec_mocks.reset_all
         end
      end
      

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多