Ruby on rails 在RSpec中使用expect和should
有以下RSpec代码:Ruby on rails 在RSpec中使用expect和should,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,有以下RSpec代码: it 'is not valid if name is not present' do @car.name = nil expect(@car).to be_invalid end 我现在读了Aaron Sumner写的“用RSpec测试”,他写的是RSpec的新风格。早些时候,我编写了以下代码: it 'is not valid if name is not present' do @car.name = nil it { should
it 'is not valid if name is not present' do
@car.name = nil
expect(@car).to be_invalid
end
我现在读了Aaron Sumner写的“用RSpec测试”,他写的是RSpec的新风格。早些时候,我编写了以下代码:
it 'is not valid if name is not present' do
@car.name = nil
it { should_not be_valid }
end
请告诉我,我说得对吗?谢谢 我认为这是一个很好的例子
describe "Car"
describe '#valid?' do
context 'when its name is nil' do
let(:car) { FactoryGirl.create(:car, :name => nil) }
it 'is not valid' do
expect(car).to_not be_valid
end
end
end
end
查找有关更好规格的更多信息使用而不是使用@car
,这样您就不会冒着在测试之间更改状态的风险,从而可能以不可预知的方式破坏测试
it
指的是主题
,因此要使你的第二种风格发挥作用,它应该是这样的:
describe '#valid?' do
let(:car) do
# make your car here
end
subject { car } # <- this is the important line to make it work...
before do
car.name = nil
end
it { should_not be_valid }
end
描述“#有效”吗
让(汽车)去做
#在这里造你的车
结束
主题{汽车}我不确定你在这里要什么?你能解释一下你的困惑或问题吗?我想知道我的规格是否有好的款式?是的。在这里: