使用RSpec编写测试有两种语法(afaik):
经典/旧方式:
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it "should not be valid" do
@micropost.should_not be_valid
end
end
失败时会出现此错误:
rspec ./spec/models/micropost_spec.rb:19当user_id为时,Micropost 不存在不应该有效
语法短:
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
失败时会出现此错误:
rspec ./spec/models/micropost_spec.rb:18#use_id时是Micropost 不存在
最后一个缺少“不应该有效”部分。
有没有办法让完整的测试失败消息,也许是我不知道的标志?
注意:无论如何都会出现更完整的错误消息:
1) Micropost when user_id is not present
Failure/Error: it { should_not be_valid }
expected #<Micropost id: nil, content: "Lorem ipsum", user_id: nil, created_at: nil, updated_at: nil> not to be valid
# ./spec/models/micropost_spec.rb:18:in `block (3 levels) in <top (required)>'
但最后的回顾是不完整的。
答案 0 :(得分:1)
您在
中看到的文字rspec ./spec/models/micropost_spec.rb:19#Micropost 1 当user_id不存在时 2 不应该有效 3
和
当user_id不存在时,rspec ./spec/models/micropost_spec.rb:18#Micropost 1 2 3
不是错误消息,而是测试名称,它是当前测试描述与其父项描述的串联:
describe Micropost do # <= 1
describe "when user_id is not present" do # <= 2
before { @micropost.user_id = nil }
it "should not be valid" do # <= 3
@micropost.should_not be_valid
end
end
end
在简短(单行)语法中 - 测试本身没有描述
describe Micropost do # <= 1
describe "when user_id is not present" do # <= 2
before { @micropost.user_id = nil }
it { should_not be_valid } # <= 3 - not described!
end
end
如果您想强制对此测试的说明,您可以这样写:
it('should not be valid') { should_not be_valid }
但这种打击单行的目的,应该是不言自明的,你不觉得吗?