2010-03-19 117 views
1

我在尝试为黄瓜铁轨模型定义一个模拟方面遇到了很多麻烦。这似乎是方法创造了一堆的消息的期望,我不断收到错误,像这样的:在黄瓜+铁轨上嘲笑问题

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) 

我还没有实现的产品分类的类,我只是希望它返回一个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

我试着使用存根很好,但有一些其他的错误,而不是...

回答

1

这真的不是REC推荐在黄瓜模拟模型。黄瓜旨在成为一个完整的堆栈,外部测试框架。

如果您还没有实现ProductCategory类,我建议从产品中删除类别关联,并测试您实现的功能。

当您开始实施ProductCategory时,您可以试着通过它来测试驱动器。

+0

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

0

我认为jrallison回答了这个问题,但无论如何,如果你觉得像在黄瓜上模拟模型来实现诸如外部连接或日期/时间操纵之类的行为,你可以使用下面的答案, 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 

祝你好运!

1

就我而言,这是因为我激活了另一个嘲笑框架,我忘记了这一点。我在规格/ 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将使用自己的模拟框架