Michael Hartl的rails教程列表6.20显示了以下代码:
before do @user = User.new(name: "Example User", email: "user@example.com") end . . . 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我无法理解这个概念,因为@ user.dup返回完全相同的对象的表示,该对象被复制到user_with_same电子邮件,但@user从未保存到文件中的任何位置的数据库中。 因此,user_with_same_email.save测试每次都应该有效。 但是,测试通过了。 有人请解释一下...... @ user = User.new(...)是否有隐式数据库保存? 我知道如果是User.create(...)会有一个保存,但不是新方法。 谢谢!
The listing 6.20 of Michael Hartl's rails tutorial shows the following code:
before do @user = User.new(name: "Example User", email: "user@example.com") end . . . 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 } endI am having trouble grasping this concept because @user.dup returns a representation of the exact same object, which is copied over to user_with_same email, but @user was never saved into the database anywhere in the file. Therefore, the user_with_same_email.save test should be valid every time. However, the test passes. Someone please explain this... is there an implicit database save on @user = User.new(...)? I know if it was User.create(...) there would be a save, but not for the new method. Thanks!
最满意答案
你没有错过隐式保存。
user_with_same_email确实正确保存(我个人总是使用save!以确保它没有默默地失败)
规范的内容是主题(即@user )无法保存,因为数据库中存在同一封电子邮件的行。
You're not missing an implicit save.
user_with_same_email does save correctly (personally I would always use save! to be sure it was not failing silently)
What the spec is speccing is that the subject (ie @user) cannot be saved, because of the existance of a row in the database with the same email.
Rails教程Rspec误解(Rails Tutorial Rspec misunderstanding)Michael Hartl的rails教程列表6.20显示了以下代码:
before do @user = User.new(name: "Example User", email: "user@example.com") end . . . 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我无法理解这个概念,因为@ user.dup返回完全相同的对象的表示,该对象被复制到user_with_same电子邮件,但@user从未保存到文件中的任何位置的数据库中。 因此,user_with_same_email.save测试每次都应该有效。 但是,测试通过了。 有人请解释一下...... @ user = User.new(...)是否有隐式数据库保存? 我知道如果是User.create(...)会有一个保存,但不是新方法。 谢谢!
The listing 6.20 of Michael Hartl's rails tutorial shows the following code:
before do @user = User.new(name: "Example User", email: "user@example.com") end . . . 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 } endI am having trouble grasping this concept because @user.dup returns a representation of the exact same object, which is copied over to user_with_same email, but @user was never saved into the database anywhere in the file. Therefore, the user_with_same_email.save test should be valid every time. However, the test passes. Someone please explain this... is there an implicit database save on @user = User.new(...)? I know if it was User.create(...) there would be a save, but not for the new method. Thanks!
最满意答案
你没有错过隐式保存。
user_with_same_email确实正确保存(我个人总是使用save!以确保它没有默默地失败)
规范的内容是主题(即@user )无法保存,因为数据库中存在同一封电子邮件的行。
You're not missing an implicit save.
user_with_same_email does save correctly (personally I would always use save! to be sure it was not failing silently)
What the spec is speccing is that the subject (ie @user) cannot be saved, because of the existance of a row in the database with the same email.
发布评论