简单的Rspec长度验证测试 - NoMethodError:未定义的方法`before'for#<rspec :: core :: examplegroup :: nested_2:0x007fb8a24bac00> </rspec :: core :: examplegroup :: nested_2:0x007fb8a24bac00>

时间:2013-10-24 20:08:39

标签: ruby-on-rails rspec

我遇到一个奇怪的问题,在我的一个模型规范中,当验证某些字段的长度时,由于此错误,我的测试没有通过。我有三个其他模型,其中规格没有任何故障问题,验证测试基本上完全相同。

进行了一些问题排查,更改了内容(例如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

1 个答案:

答案 0 :(得分:3)

之前的块应该在之前运行之后的所有测试,因此它必须超出它的块。如果代码特定于一个场景,那么只需删除之前,因为它没有真正做任何事情。

it "is not valid when title is too long" do
  question = FactoryGirl.create(:question)
  question.title = "a" * 101
  question.should_not be_valid
end