我可以在这里看到两个小问题。首先使用let语法在 rspec 中定义记忆变量。其次,在构建散列时,将*_path助手周围的括号去掉。
所以说:
lol = Hash[ 'Profile' => 'user_path(user)',
'Sign out' => 'signin_path']
有:
let(:lol) { Hash[ 'Profile' => user_path(user),
'Sign out' => signin_path] }
您的描述块可能是:
describe "with valid information" do
let(:lol) { Hash[ 'Profile' => user_path(user),
'Sign out' => signin_path] }
it { should have_list_of_links(lol) }
end
作为副作用,我将向您展示一个小例子。鉴于您在$PROJECT/spec/support/utilities.rb文件中定义了匹配器,应用程序路由等...已正确设置,并且您的视图中有链接。
describe "Users pages" do
before { visit root_path }
let(:values) { Hash['Index' => users_path,
'Sign out' => signout_path,
'Sign in' => signin_path] }
subject { page }
describe "with valid informations" do
it { should have_list_of_links(values) }
end
end
运行 rspec:
> rspec
.
Finished in 0.00267 seconds
1 example, 0 failures
Randomized with seed 67346
运行 rspec -f 文档
>rspec -f documentation
Users pages
with valid informations
should have link "Sign in"
Finished in 0.00049 seconds
1 example, 0 failures
Randomized with seed 53331
这不清楚和误导,特别是文档开关。在新应用程序上运行 rspec -f 文档是一种常见的做法,您只需动手(如果他们使用 rspec ofc)。为了更好地了解正在发生的事情。
如果您有:
describe "Users pages" do
before { visit root_path }
subject { page }
describe "with valid informations" do
it { should have_link('Index', href: users_path) }
it { should have_link('Sign out', href: signout_path) }
it { should have_link('Sign in', href: signout_path) }
end
end
运行 rspec:
>rspec
...
Finished in 0.0097 seconds
3 examples, 0 failures
Randomized with seed 53347
运行 rspec -f 文档
>rspec -f documentation
Users pages
with valid informations
should have link "Index"
should have link "Sign out"
should have link "Sign in"
Finished in 0.00542 seconds
3 examples, 0 failures
Randomized with seed 40120
我个人喜欢第二种情况(更详细的一种)。随着测试数量的增加和测试结构变得越来越复杂,它的价值也在增加。您可以简单地运行rspec -f 文档来学习如何使用应用程序,甚至无需阅读用户手册/教程。