2014-03-04 19 views
6

我有一個測試情況是這樣的:方法instance_double :: ExampleMethods

describe WorkCardsController do 
    it "something" do 
     work_card = instance_double(WorkCard, {:started?=>true}) 
     #some more code 
    end 
end 

當我運行RSpec的,我得到一個錯誤:

undefined method 'instance_double' for #<Rspec::Core::ExampleGroup::Nested_1::Nested_8::Nested_3:0x007f0788b98778> 

根據http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods這方法存在。於是,我就直接訪問它:

describe WorkCardsController do 
    it "something" do 
     work_card = RSpec::Mocks::ExampleMethods::instance_double(WorkCard, {:started?=>true}) 
     #some more code 
    end 
end 

然後我得到了一個非常驚人的錯誤:

undefined method 'instance_double' for Rspec::Mocks::ExampleMEthods:Module 

這違背了我上面鏈接的文檔。

我錯過了什麼?

+0

確實有rspec3?這一刻的寶石版本是2.14,因此如果你沒有通過github安裝它,這是正常的,方法不存在。 – Iazel

回答

1

從你指着文檔:

Mix this in to your test context (such as a test framework base class) to use rspec-mocks with your test framework.

嘗試include到你的代碼:

include RSpec::Mocks::ExampleMethods 

你直接的方法失敗,因爲調用

RSpec::Mocks::ExampleMethods::instance_double(...) 

預計,方法被聲明爲類方法:

def self.instance_double(...) 

但它已被聲明爲實例方法:

def instance_double(...) 
相關問題