我正在学习与Rails Tutorial的Rails。作者主动教授RSpec的选择位。在每章之后的练习中,您有机会学习编写密码。我选择了这些练习。但是,问题在于,在接下来的章节中,他将读者视为他们没有看到的东西,所以当你不知道如何适应他写的新的“不定式”代码时,你写的密码本身就会带来挑战这是'精明'的副本。Ruby on Rails RSPEC
在精粹版,文件的开头适合于少重复,看起来这样:
require 'spec_helper'
describe User do
before { @user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar") }
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation)}
it { should be_valid }
.
.
.
end
这样,那么我们的测试看起来像这样:
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
尝试当创建一个测试是否存在@user.password
和@user.password_confirmation
,我期待继续这种RSpec格式。
作者版本看起来是这样的:
describe "when email is not present" do
before do
@user= User.new(name: "Example User", email: "[email protected]",
password: " ", password_confirmation = " ")
end
it { should_not be_valid }
end
我在它的尝试(因为提交恢复到做事而不pithier RSpec的代码运动的方法)将是这样:
describe "when password is not present" do
before { @user.password = " ", @user.password_confirmation = " " }
it { should_not be_valid }
end
这是在此上下文中使用before之前修改多个哈希值的正确方法吗?
此外,我正在努力找到我RSpec问题的答案。有没有人有寻找这些答案的可靠程序?
太棒了,谢谢,这很有帮助!我正在使用水豚,所以我会看看他们的文档。另外,您是否需要用'{}'包围单行块,还是可以将它们关闭? – EnergyWasRaw