以下代码:
describe Task do
let(:task) { Task.new }
subject { task }
before(:each) do
task.valid?
p task.errors.full_messages
end
it { should have(1).error_on(:title) }
it { should have(1).error_on(:description) }
end
输出:["Title can't be blank", "Title can't be blank", "Title can't be blank"]
我不知道发生这种情况的任何具体原因,但我体验事情的方式是 let 和 subject 不能很好地工作,哈哈
即使以下代码也会产生相同的错误:
it "should have 1 error on title" do
Task.new.should have(1).error_on(:title)
end
it "should have 1 error on description" do
Task.new.should have(1).error_on(:description)
end