我正在学习rails和rspec,并没有理解这两个代码之间的区别:
在我的spec / models / user_spec.rb中我有:
describe User do$
before { @user = User.new(name: "Example User", email: "user@example.com") }
subject {@user}
现在我检查名称格式:
describe "when name has wrong format" do
it "should not be valid" do
names = %w[%marcin ??? 123 ___123 ___]
names.each do |invalid_name|
@user.name = invalid_name
it{should_not be_valid}
end
end
end
它不起作用但是当我使用@user.should_not be_valid
而不是it{should_not be_valid}
时它可以正常工作,我没有理解为什么我subject {@user}
所以我认为it {should
意味着与@user.should
答案 0 :(得分:1)
describe User do
subject do
User.new(name: name, email: "user@example.com")
end
describe "#name" do
context "Just Numbers" do
let(:name) { "123" }
it { should_not be_valid }
end
end
end