我创建了两个非常相似的表。其中一个叫做表演者,另一个是表演。 Performer只包含一个名称:string。 Performance包含file_name:string,date:date和location:string。我有两个非常相似的rspec文件和Performer测试通过没有问题,但性能未通过所有3个属性的测试。当我输入沙箱并使用
测试属性时 performance = Performance.new
performance.respond_to?(:file_name)
它返回true。
然而在rspec中失败了。 这是失败。
1) Performance
Failure/Error: it { should respond_to(:date) }
expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :date
# ./spec/models/performance_spec.rb:10:in `block (2 levels) in <top (requir
ed)>'
2) Performance
Failure/Error: it { should respond_to(:file_name) }
expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :file_name
# ./spec/models/performance_spec.rb:9:in `block (2 levels) in <top (require
d)>'
3) Performance
Failure/Error: it { should respond_to(:location) }
expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :location
# ./spec/models/performance_spec.rb:11:in `block (2 levels) in <top (requir
ed)>'
这是我的rspec文件:
require 'spec_helper'
describe Performance do
before { @performance = Performance.new(file_name: "Example Performece",
date: DateTime.parse("2011-06-02T23:59:59+05:30").to_date,
location: "lame house", ) }
subject { Performance }
it { should respond_to(:file_name) }
it { should respond_to(:date) }
it { should respond_to(:location) }
end
我的表演者文件非常相似,日期属性是否会以某种方式弄乱这件事?我尝试使用Performance.new创建一个空的性能,但它没有解决问题,所以我不知道发生了什么。
如果你想在下面看到我的文件评论并在这里发布。我在电脑旁,所以我应该快速回复
答案 0 :(得分:2)
使用单行语法时:
it { should have_something }
就像你写的那样
it 'should have something' do
subject.should have_something
end
在您的代码中,您声明主题是类 Performance
:
subject { Performance }
这意味着您的测试实际上是:
it 'should respond to file_name' do
Performance.should respond_to(:file_name)
end
当然,它没有。
将主题设置为@performance
subject { @performance }
测试成为:
it 'should respond to file_name' do
@performance.should respond_to(:file_name)
end
应该按预期工作。
答案 1 :(得分:1)
subject { Performance.new(file_name: "Example Performece", date: DateTime.parse("2011-06-02T23:59:59+05:30").to_date, location: "lame house", ) }
全部