加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RSpec定制匹配黄瓜到DRY实现相关的测试,是否可

发布时间:2020-12-17 02:54:03 所属栏目:百科 来源:网络整理
导读:我正在阅读Michael Hartl撰写的新版“Rails Tutorial”,因为我非常喜欢使用Cucumber的BDD,我发现自己很担心作者在这里指出的内容: http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#sec:rspec_custom_matchers 简而言之,与Cucumber的
我正在阅读Michael Hartl撰写的新版“Rails Tutorial”,因为我非常喜欢使用Cucumber的BDD,我发现自己很担心作者在这里指出的内容: http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#sec:rspec_custom_matchers

简而言之,与Cucumber的主要麻烦在于,不可能像以下那样执行依赖于实现的测试:

Then /^he should see an error message$/ do
  page.should have_selector('div.alert.alert-error',text: 'Invalid')
end

写这样的RSpec自定义匹配器:

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-error',text: message)
  end
end

因为这样的自定义匹配器必须放在spec / support / utilities.rb中,并且可以从RSpec集成测试调用,但不能从Cucumber步骤定义调用.

你是积极的/你对此有何看法?

谢谢.

解决方法

您可以将依赖于实现的或可重用的方法,定位器放到Cucumber World中.

您的方案示例:

# step_definitions/general_steps.rb
Then /^he should see an error message "(.+)"$/ do |text|
  within(error_message) do
    page.should have_content(text)
  end
end

# support/general_helpers.rb
module GeneralHelpers
  def error_message
    page.first('div.alert.alert-error')
  end
end

World(GeneralHelpers)

以下是一些引用此方法的文章:

> It’s time to clean up your mess: refactoring Cucumber step definitions
> Using Cucumber World

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读