2011-01-29 56 views
4

當運行:直接在存根上的錯誤模型結果上調用rspec!方法

rake spec:models 

一切正常,但是當我做

rspec spec/models/spot_spec.rb 

Spot.stub! :test1,我得到:

undefined method `stub!' for Spot:Class 

該錯誤只發生在我包括存根!線。

任何想法如何避免它?我只想運行特定型號的規格。

更新:

使用Ruby 1.9.2和RSpec 2.4.0,這裏是spot_spec.rb代碼:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 

describe Spot do 
    before(:all) do 
    Spot.stub! :test1 
    @spot = Spot.new 
    end 

    subject {@spot} 

    describe "validations" do 
    it { should validate_presence_of(:user) } 
    end 
end 

而且spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 
    config.mock_with :rspec 
end 
+0

請向我們展示`spot_spec.rb`的完整代碼。 – 2011-01-29 11:03:02

回答

14

橫空出世issue in before(:all)致電:

這是正確的。嘲笑隱含在 之後(:每個) 驗證並清除,所以它們不會在之前(:全部)中工作。

更改爲before(:each)解決了它。

謝謝大家。

1

有spot_spec.rb包含spec_helper.rb,然後確保spec_helper.rb包含spot_spec.rb。

如果您正在運行紅寶石1.9+,你可以使用require_relative包括spot_spec.rb在spec_helper.rb

更新:

在spec_helper.rb加:

require_relative '../app/models/spot' 
+0

我已經更新了上面的代碼,請您指出準確地做了什麼? – khelll 2011-01-29 11:22:16