未定义的方法`mock_model'

时间:2011-12-20 08:35:37

标签: ruby-on-rails rspec rspec-rails

我正在尝试将Rspec 1.3.1用于我的rails应用程序,它运行在2.3.8上。我可以使用stub_model方法“存根”模型。但是当我调用mock_model时,事情就出错了,这就是我得到的堆栈跟踪

./spec/models/bucket_spec.rb:32: undefined method `mock_model' for Spec::Rails::Example::ModelExampleGroup::Subclass_2:Class (NoMethodError)
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:188:in `module_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:188:in `subclass'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:55:in `describe'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_factory.rb:31:in `create_example_group'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/dsl/main.rb:28:in `describe'
from ./spec/models/bucket_test.rb:31
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:15:in `load'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:14:in `each'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/options.rb:134:in `run_examples'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/command_line.rb:9:in `run'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/bin/spec:5
from /usr/local/bin/spec:19:in `load'
from /usr/local/bin/spec:19

bucket_spec.rb文件:

  require 'spec_helper'
   describe Bucket, "creation" do
  before(:each) do
    @bucket = stub_model(Bucket, :id => 1, :name => "Below Proficient", :color =>     "green", :min_range => 0, :max_range => 30, :class_group_id => 1).as_new_record
  end
  it "should be valid with all the attributes set to some randowm values" do
    @bucket.should be_valid
  end
  it "should be valid without min_range" do
    @bucket.min_range = nil
    @bucket.should be_valid
   end
  it "should be valid without max_range" do
    @bucket.max_range = nil
    @bucket.should be_valid
  end
  it "should be valid without class_group_id" do
    @bucket.class_group_id = nil
    @bucket.should be_valid
  end
  it "should not be valid without color" do
    @bucket.color = nil
    @bucket.should_not be_valid
  end
  it "should not be valid without name" do
    @bucket.name = nil
    @bucket.should_not be_valid
  end
end

describe Bucket, "saving" do
  @bucket = mock_model(Bucket)
  @bucket.should be_valid
end

spec_helper.rb文件:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'spec/rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
#
#RSpec.configure do |config|
#  # == Mock Framework
#  #
#  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#  #
#  # config.mock_with :mocha
#  # config.mock_with :flexmock
#  # config.mock_with :rr
#  config.mock_with :rspec
#
#  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#  config.fixture_path = "#{::Rails.root}/spec/fixtures"
#
#  # If you're not using ActiveRecord, or you'd prefer not to run each of your
#  # examples within a transaction, remove the following line or assign false
#  # instead of true.
#  config.use_transactional_fixtures = true
#end
Spec::Runner.configure do |config|
  config.mock_with :rspec
  config.use_transactional_fixtures = true
end

rspec gems列表

 gem list rspec

 *** LOCAL GEMS ***

 rspec (1.3.1)
 rspec-rails (1.3.3)

rails gems列表

 gem list rails

 *** LOCAL GEMS ***

 rails (2.3.8, 2.3.5)

1 个答案:

答案 0 :(得分:7)

您对mock_model的调用位于描述块的顶层,这是没有意义的

你只能在before(:each)里面做一个例子(例如在传递给it的块中)和那样的地方

相关问题