0

我有一个奇怪的问题,在我的一个模型规范中,当验证某些字段的长度时,由于这个错误,我的测试没有通过。我还有其他三个模型,它们的规格没有任何失败问题,并且验证测试的编写基本完全相同。

解决了一些问题,改变了一些东西(例如将 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
4

1 回答 1

3

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

it "is not valid when title is too long" do
  question = FactoryGirl.create(:question)
  question.title = "a" * 101
  question.should_not be_valid
end
于 2013-10-24T20:11:52.057 回答