rspec 模拟“未定义的方法‘stub_model’”对于#<类 :0x007ff9c339bd80>(无方法错误)”类>
我正在使用 Rails 3.1,我想在我的规范中添加一些存根和模拟,但我得到了 NoMethodError:
undefined method `stub_model' for #<Class:0x007ff9c339bd80> (NoMethodError)
这是我的 GemFile 的摘录:
gem 'rspec'
gem 'rspec-rails'
我运行了捆绑安装和rails g 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”和“bundle exec rspec ./spec”(都尝试过,没有区别
)我所做的似乎是教科书(事实上,我正在遵循 The Rails 3 Way)。
我缺少什么?
I'm using Rails 3.1 and I wanted to add some stubs and mocks to my specs but I get a NoMethodError:
undefined method `stub_model' for #<Class:0x007ff9c339bd80> (NoMethodError)
Here is an excerpt of my GemFile:
gem 'rspec'
gem 'rspec-rails'
I ran bundle install and rails g rspec:install
And here is the code that tries to create a stub_model
0 @flight = stub_model(Flight)
1 Flight.stub! (:all).and_return([@flight])
And here is 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
I'm calling "rspec ./spec" and "bundle exec rspec ./spec" (tried both, no difference)
Everything I'm doing seems to be textbook (in fact, I'm following The Rails 3 Way).
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的原始代码很可能是在规范示例之外运行的。这将给出您所描述的错误:
但这将起作用:
Chances are that your original code was being run outside of a spec example. This will give the error you describe:
but this will work:
请参阅原始问题中的评论。就像我脖子上的一个坏结一样,它似乎已经自行解决了。
See comment in original question. Like a bad knot in my neck, it seems to have worked itself out.