我遵循Michael Hartl的Ruby on Rails教程。我觀察到他用minitest進行測試。我使用RSpec。下面是代碼的教程線我要使用RSpec什麼是assert(minitest)等效的RSpec語法
test "email validation should accept valid addresses" do
valid_addresses = %w[[email protected] [email protected] [email protected]
[email protected] [email protected]]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
複製這是我怎麼會做這樣的使用RSpec。
it "email validation should accept valid addresses" do
valid_addresses = ["[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]"]
valid_addresses.each do |valid_address|
user.email = valid_address
expect(@user.valid?).to be true, "#{valid_address.inspect} is not correct"
end
end
但是,我運行此測試時出現參數錯誤。看起來RSpec在測試用例中只需要一個參數。
這是一個錯字@margo,'''user''應該是'''@ user'''。錯誤信息是:'''ArgumentError: 錯誤的參數數量(給定2,預期1)'''' –