我在这里可以看到两个小问题。首先使用let语法在rspec中定义memoized变量。其次,当你建立你的散列时,请在括号里面加上* _path助手。
所以insted的:
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文档以了解如何使用应用程序,而无需使用用户手册/教程。
你是什么意思**我知道这是不应该做的方式**通过看它应该工作,做你所期望的代码。你有任何错误?我可以看到的唯一副作用是所有链接都将在单个示例中运行,并且可能不适用于文档。 – Kocur4d 2013-03-20 02:52:50
其实我不知道这是否是做了正确的方式,这更是我想说的... – jon 2013-03-20 08:56:11
我有一个错误信息,我只好等到工作日复制的结束/粘贴这里。如果我明白你提到的副作用,我不确定。你会不会推荐这种实施测试的方式?但无论如何,最终我的目标只是练习一些不在tuto中编写的代码。 – jon 2013-03-20 09:03:05