我尝试练习使用Rspec进行一些测试,我有一个奇怪的comportment。当我尝试使用无效模型来跟随Red / Green / Refactor循环时,Rspec看不到任何错误。
我想确保释放不能有前期。
我的模特
class Release < ActiveRecord::Base
belongs_to :game
belongs_to :platform
attr_accessible :date
validates :date, presence: true
end
我的规范文件
require 'spec_helper'
describe Release do
before {@release = Release.new(date: Time.new(2001,2,3))}
it{should respond_to :date}
it{should respond_to :game}
it{should respond_to :platform}
describe "when date is not present" do
before {@release.date = nil}
it {should_not be_valid}
end
describe "when date is anterior" do
before {@release.date = Time.now.prev_month}
it {should_not be_valid}
end
end
我的输出
.....
Finished in 0.04037 seconds
5 examples, 0 failures
有什么想法吗?
答案 0 :(得分:2)
当您编写it { should_not be_valid }
时,您似乎认为接收方是@release
(rspec将如何知道?),但默认情况下,隐式对象是类describe
的实例d:
https://www.relishapp.com/rspec/rspec-core/docs/subject/implicit-receiver
将subject { some_object }
用于明确的主题或it { @release.should_not be_valid }
。
更多相关内容:
http://blog.davidchelimsky.net/2012/05/13/spec-smell-explicit-use-of-subject/
答案 1 :(得分:1)
尝试:
it { @release.should_not be_valid}
代替。