这来自通过示例书学习 Rails:
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
@user.should_not be_valid
end
end
我是Rspec的初学者。之前有这样一段代码:
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
所以我想知道为什么作者没有这样写上面的代码:
describe "when email format is invalid" do
before do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
@user.email = invalid_address
addresses.each do |invalid_address|
@user.email = invalid_address
end
end
it { should_not be_valid }
end
好的,也许这段代码是错误的。但基本上,我在问为什么作者没有使用before
beforeit
或将address
变量放在 before it
?(就像他在前面的例子中所做的那样)
编辑:
他还在另一个例子中做了类似的事情:
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end