0
我有一個奇怪的問題,其中在我的模型規範之一,驗證長度對於某些領域,我的測試不會因爲這個錯誤而通過。我有另外三個模型,其中規範沒有任何失敗問題,驗證測試基本上完全相同。簡單的Rspec長度驗證測試 - NoMethodError:未定義的方法`之前'爲#<RSpec :: Core :: ExampleGroup :: Nested_2:0x007fb8a24bac00>
排除故障有點改變(例如,spec_helper.rb包含config.include Capybara::DSL
),但仍然有相同的問題。
這裏是我的測試中questions_spec.rb
:
it "is not valid when title is too long" do
question = FactoryGirl.create(:question)
before {question.title = "a" * 101 }
it { should_not be_valid }
end
it "is not valid when brief is too long" do
question = FactoryGirl.build(:question)
before { question.brief = "a" * 501 }
question.should_not be_valid
end
我的錯誤:
Failures:
1) Question is not valid when title is too long
Failure/Error: before {question.title = "a" * 101 }
NoMethodError:
undefined method `before' for #<RSpec::Core::ExampleGroup::Nested_2:0x007fb89df51c90>
# ./spec/models/question_spec.rb:36:in `block (2 levels) in <top (required)>'
2) Question is not valid when brief is too long
Failure/Error: before { question.brief = "a" * 501 }
NoMethodError:
undefined method `before' for #<RSpec::Core::ExampleGroup::Nested_2:0x007fb8a24bac00>
# ./spec/models/question_spec.rb:41:in `block (2 levels) in <top (required)>'
而且我spec_helper.rb
:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'foreigner-matcher'
# 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 }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
#add Capybara commands
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
# Clean up the database
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
# ## 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
# 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 = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
編輯:這是我的idea_spec.rb
測試對與之配合相同的格式。
describe "when title is too long" do
idea = FactoryGirl.create(:idea)
before {idea.title = "a" * 101 }
it { should_not be_valid }
end
describe "when brief is too long" do
idea = FactoryGirl.create(:idea)
before { idea.brief = "a" * 501 }
it { should_not be_valid }
end
謝謝,我想最後有點愚蠢的問題。我只是困惑,因爲之前在其他測試中工作過,我在其他模型驗證中使用了相同的格式?我不明白爲什麼這個錯誤只會出現在一個模型下。 – Annika
如果你發佈一個它工作的地方,我會告訴你它們有什麼區別:) –
它的工作原理是因爲你正在使用一個describe塊而不是它的塊。 –