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

ruby-on-rails – 在RSpec中有一个等同于Cucumber的“场景”,还

发布时间:2020-12-16 20:23:13 所属栏目:百科 来源:网络整理
导读:对于黄瓜情景的简洁和实用,我印象深刻,它们是测试不同情况的好方法. 例如例如黄瓜情景 Feature: Manage UsersIn order to manage user detailsAs a security enthusiastI want to edit user profiles only when authorizedScenario Outline: Show or hide ed
对于黄瓜情景的简洁和实用,我印象深刻,它们是测试不同情况的好方法.

例如例如黄瓜情景

Feature: Manage Users
In order to manage user details
As a security enthusiast
I want to edit user profiles only when authorized

Scenario Outline: Show or hide edit profile link
  Given the following user records
    | username | password | admin |
    | bob      | secret   | false |
    | admin    | secret   | true  |
  Given I am logged in as "<login>" with password "secret"
  When I visit profile for "<profile>"
  Then I should <action>

  Examples:
    | login | profile | action                 |
    | admin | bob     | see "Edit Profile"     |
    | bob   | bob     | see "Edit Profile"     |
    |       | bob     | not see "Edit Profile" |
    | bob   | admin   | not see "Edit Profile" |

(代码取自Ryan Bates’ More on Cucumber Screencast)

RSpec有相当的等级吗?

我想在RSpec中做同样的事情,并通过在场景表中将不同的测试减少到一行来减少我的代码.

虽然我可以自己编写代码,但我正在考虑的事实让我想知道两件事情

>如果这是有用的,它可能已经存在,在哪种情况下如何使用它?
>如果不存在,它表明不应该这样做,而且我不正确地接近问题,我应该如何反思我的RSPec方法?

哪个答案是正确的,如果它是有用的,我该如何做?

解决方法

尝试以下方法.我喜欢原来的方式.
describe StateDateMethods do
  before :each do
    @product = OpenStruct.new
    @product.extend StateDateMethods
  end

  def parse_date(unparsed_date_value)
    unless unparsed_date_value.nil?
      DateTime.strptime(unparsed_date_value,'%m/%d/%Y')
    end
  end

  context '#pre_order?' do
    examples = [
      # [visible_on,pre_order_on,for_sale_on] => method_result
      { :inputs => [nil,nil,nil],:expected => false },{ :inputs => ['1/1/2001','1/1/2001',:expected => true },'1/2/2001','1/2/2001'],'1/1/3001'],'1/1/3001','1/2/3001'],{ :inputs => ['1/1/3001','1/1/2001'],:expected => false }
    ]
    examples.each do |example|
      inputs = example[:inputs]

      it "should return #{example[:expected].inspect} when visible_on == #{inputs[0].inspect},pre_order_on == #{inputs[1].inspect},for_sale_on == #{inputs[2].inspect}" do
        @product.visible_on = parse_date(inputs[0])
        @product.pre_order_on = parse_date(inputs[1])
        @product.for_sale_on = parse_date(inputs[2])

        @product.pre_order?.should == example[:expected]
      end
    end
  end
end

我认为这给了两个世界最好的,因为它让我不再重复,并且为每个条件创建了一个不同的测试.

这是一个失败的样子:

....F.....

Failures:

  1) StateDateMethods#pre_order? should return false when visible_on == "1/1/2001",pre_order_on == "1/1/2001",for_sale_on == "1/2/2001"
     Failure/Error: @product.pre_order?.should == example[:expected]
       expected: false
            got: true (using ==)
     # ./spec_no_rails/state_date_methods_spec.rb:40:in `block (4 levels) in <top (required)>'

Finished in 0.38933 seconds
10 examples,1 failure

Failed examples:

rspec ./spec_no_rails/state_date_methods_spec.rb:35 # StateDateMethods#pre_order? should return false when visible_on == "1/1/2001",for_sale_on == "1/2/2001"

这就是所有绿色的样子:

..........

Finished in 0.3889 seconds
10 examples,0 failures

(编辑:李大同)

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

    推荐文章
      热点阅读