【发布时间】:2011-09-22 06:42:47
【问题描述】:
我正在使用 Rails 3.1,我想在我的规范中添加一些存根和模拟,但我得到了 NoMethodError:
undefined method `stub_model' for #<Class:0x007ff9c339bd80> (NoMethodError)
这是我的 GemFile 的摘录:
gem 'rspec'
gem 'rspec-rails'
我运行了 bundle install 和 rails g rspec:install
这是尝试创建 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”和“bundle exec rspec ./spec”(都试过了,没有区别)
我所做的一切似乎都是教科书(实际上,我正在遵循 The Rails 3 Way)。
我错过了什么?
【问题讨论】:
-
从哪里调用 stub_model?控制器规格?视图规范?型号规格?
-
嗯...它刚刚决定开始工作。我建立了一个新项目来测试其中的行为,并且在该项目中运行良好。然后我再次尝试了原始项目,效果很好。很奇怪。
-
在你失败的代码周围多写几行上下文可能会让错误更早地被别人看出来:)
标签: ruby-on-rails rspec ruby-on-rails-3.1 rspec-rails