【问题标题】:ruby rails create have_list_of_links utility for rspec testruby rails 为 rspec 测试创建 have_list_of_links 实用程序
【发布时间】:2013-03-19 20:30:10
【问题描述】:

我是 ruby​​/rails 的新手,正在做一个教程,我想知道如何解决我的小问题...

我有一个描述“使用有效信息”,它测试当用户使用有效信息登录时应该或不应该发生什么。 在此过程中,我希望能够验证新页面上是否存在某些链接。在 tuto 中,只有 2 个链接,所以我们这样做:

it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }

但是如果我们假设有 10 个链接呢?我认为有一个助手会更容易吗?但是我不知道怎么写。这就是我带来的......

 lol = Hash[ 'Profile'  => 'user_path(user)',
             'Sign out' => 'signin_path']
 it { should have_list_of_links(lol) }

然后在 $PROJECT/spec/support/utilities.rb 文件中,我定义了新的 *have_list_of_links* 函数:

RSpec::Matchers.define :have_list_of_links do |lol|
  match do |page|
    lol.each_pair do |label, link| 
                page.should have_link(label, href: link) 
    end
  end
end

我知道这不是正确的做法,但我不知道该怎么做...

谢谢。

【问题讨论】:

  • 你的意思是什么我知道这不是正确的做法通过查看它应该工作的代码并做你期望的事情。你有任何错误吗?我能看到的唯一副作用是所有链接都将在一个示例中运行,这可能不利于文档。
  • 其实我不知道这样做是否正确,这就是我想说的更多......
  • 我收到一条错误消息,我只需要等到工作日结束才能在此处复制/粘贴。我不确定我是否理解你提到的副作用。您不会推荐这种实施测试的方式吗?但无论如何,最后我的目标只是练习一些教程中没有写的代码。

标签: ruby-on-rails ruby helper utility matcher


【解决方案1】:

我可以在这里看到两个小问题。首先使用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 文档 来学习如何使用应用程序,甚至无需阅读用户手册/教程。

【讨论】:

  • 超级,感谢您的速成课程!今晚我将测试您的解决方案并分享结果。还有一个问题:RSpec 和行为驱动编程真的在网络公司中使用,还是更理想的方式?
  • 好公司测试,坏公司不测试。他们正在测试的内容(RSpec/Minitest/其他框架)只是个人喜好。如果我说 Rspec 和 BDD 是“最好的”,你应该在 UnitTest 和 TDD 上使用它我喝一杯:) 底线是 - 你测试什么并不重要。只是测试:)。
  • 好的,再次感谢您的信息,我的东西现在可以正常工作了。我还发现了一个非常相似的帖子stackoverflow.com/questions/11916962/…,它显示的视图略有不同。我想我需要继续尝试一些“实验”,我肯定会在一些时候回到这里。 ciao
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多