rspec 模拟“未定义的方法‘stub_model’”对于#<类 :0x007ff9c339bd80>(无方法错误)”

发布于 2024-12-05 21:28:41 字数 1708 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-12-12 21:28:41

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

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

Chances are that your original code was being run outside of a spec example. This will give the error you describe:

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

but this will work:

class Foo; end
describe Foo do
  before do
    @foo = stub_model(Foo)
    Foo.stub!(:all).and_return([@foo])
  end
end
晨光如昨 2024-12-12 21:28:41

请参阅原始问题中的评论。就像我脖子上的一个坏结一样,它似乎已经自行解决了。

See comment in original question. Like a bad knot in my neck, it seems to have worked itself out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文