2011-12-20 59 views
2

我試圖使用Rspec 1.3.1爲我的rails應用程序運行在2.3.8上。我可以用stub_model方法'存根'模型。但是,當我打電話mock_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寶石清單

gem list rspec 

*** LOCAL GEMS *** 

rspec (1.3.1) 
rspec-rails (1.3.3) 

軌道名單寶石

gem list rails 

*** LOCAL GEMS *** 

rails (2.3.8, 2.3.5) 
+0

爲什麼你使用這樣一箇舊版本的Rspec? – apneadiving 2011-12-20 09:36:57

+0

新版本的Rspec會使用舊版本的Rails嗎? – Vineeth 2011-12-20 09:44:41

+0

不,你是對的,抱歉 – apneadiving 2011-12-20 09:46:22

回答

7

您對mock_model呼叫在您的描述塊的頂層這沒有任何意義

你只能做內前(:每個),一個例如(即在傳遞給it的塊中)以及類似的地方

+0

賓果!這樣可行。 – Vineeth 2011-12-21 04:09:11