2011-09-22 54 views
0

我用Rails 3.1,我想补充一些存根和嘲笑我的规格,但我得到一个NoMethodError:RSpec的嘲弄“未定义的方法`stub_model”为#<类别:0x007ff9c339bd80>(NoMethodError)”

undefined method `stub_model' for #<Class:0x007ff9c339bd80> (NoMethodError) 

这里是我的Gemfile的摘录:

gem 'rspec' 
gem 'rspec-rails' 

我跑捆绑安装和轨道摹rspec的:安装

这里是试图创建一个stub_model代码

0  @flight = stub_model(Flight) 
    1  Flight.stub! (:all).and_return([@flight]) 

这里是spec_helper.rb:

0 # This file is copied to spec/ when you run 'rails generate rspec:install' 
    1 ENV["RAILS_ENV"] ||= 'test' 
    2 require File.expand_path("../../config/environment", __FILE__) 
    3 require 'rspec/rails' 
    4 
    5 # Requires supporting ruby files with custom matchers and macros, etc, 
    6 # in spec/support/ and its subdirectories. 
    7 Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 
    8 
    9 RSpec.configure do |config| 
10 # == Mock Framework 
11 # 
12 # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
13 # 
14 # config.mock_with :mocha 
15 # config.mock_with :flexmock 
16 # config.mock_with :rr 
17 config.mock_with :rspec 
18 
19 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
20 config.fixture_path = "#{::Rails.root}/spec/fixtures" 
21  
22 # If you're not using ActiveRecord, or you'd prefer not to run each of your 
23 # examples within a transaction, remove the following line or assign false 
24 # instead of true. 
25 config.use_transactional_fixtures = true 
26 end 

我打电话 “rspec的./spec” 和 “捆绑的exec rspec的./spec”(尝试都没有区别)

我所做的一切似乎都是教科书(实际上,我遵循Rails 3 Way)。

我错过了什么?

+0

凡从叫stub_model?控制器规格?查看规格?型号规格? –

+0

控制器规格 – hamiltop

+0

嗯......它只是决定开始工作。我建立了一个新项目来测试行为,并且在该项目中运行良好。然后,我再次尝试了原始项目,并且工作正常。奇怪的。 – hamiltop

回答

0

查看原文。评论原文。就像我脖子上的一个坏结,似乎已经自行解决了。

2

很可能是您的原始代码在规范示例外运行。这会给你的错误描述:

class Foo; end 
describe Foo do 
    @foo = stub_model(Foo) 
    Foo.stub!(:all).and_return([@foo]) 
end 

,但是这将工作:

class Foo; end 
describe Foo do 
    before do 
    @foo = stub_model(Foo) 
    Foo.stub!(:all).and_return([@foo]) 
    end 
end 
相关问题