Ruby on rails 直接在模型上调用rspec会导致存根中出现错误!方法

Ruby on rails 直接在模型上调用rspec会导致存根中出现错误!方法,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,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.

运行时:

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

让spot_spec.rb包含spec_helper.rb,然后确保spec_helper.rb包含spot_spec.rb

如果您运行的是ruby 1.9+,那么可以使用require\u relative将spot\u spec.rb包含在spec\u helper.rb中

更新:

在spec_helper.rb中添加:

require_relative '../app/models/spot'
原来是(:all)调用之前的一个in

没错。模拟是隐式的 在(:每个)之后验证并清除 所以他们以前不会在这里工作

将它更改为(:each)
之前的
,解决了它


谢谢大家。

请给我们看一下
spot_spec.rb
的完整代码。我已经更新了上面的代码,请您指出具体的操作方法好吗?