1
我是Rails的新手,並試圖用TDD和BDD編寫應用程序。RSpec和ActiveRecord:示例在無效方案上失敗
現在在其中一個模型中有一個字段長度驗證。有一個RSpec有一個例子來檢查這個特定字段的長度驗證。
下面是Model類
class Section < ActiveRecord::Base
# Validations
validates_presence_of :name, length: { maximum: 50 }
end
和RSpec
require 'spec_helper'
describe Section do
before do
@section = Section.new(name:'Test')
end
subject { @section }
# Check for attribute accessor methods
it { should respond_to(:name) }
# Sanity check, verifying that the @section object is initially valid
it { should be_valid }
describe "when name is not present" do
before { @section.name = "" }
it { should_not be_valid }
end
describe "when name is too long" do
before { @section.name = "a" * 52 }
it { should_not be_valid }
end
end
當我敲響該規範例如失敗,以下錯誤
....F......
Failures:
1) Section when name is too long
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/section_spec.rb:24:in `block (3 levels) in <top (required)>'
Finished in 0.17311 seconds
11 examples, 1 failure
我失去了一些東西在這裏?
另外請建議我一些參考,以瞭解如何使用RSpec(和Shoulda)測試模型,尤其是關係。
謝謝哥們。錯誤得到解決。我正在使用Rails 3.2.2並使用第二種解決方案。 – 2012-03-17 18:30:29