我正在尽我所能了解 Rspec 的流程,但最简单的事情似乎让我无法理解。
我想在两个不同的模型上重用一些属性格式有效性的测试。所以,我想我会将那些 Rspec 测试转移到另一种方法中,然后只用被测对象调用该方法。即从:
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
end
对于这样的事情:
describe "email format checking" do
valid_email_check(@user)
end
..和实用程序.rb
def valid_email_check(subject)
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|
subject.email = invalid_address
subject.should_not be_valid
end
end
end
end
传递给此函数时,“subect”始终为零。任何帮助表示赞赏。