gpt4 book ai didi

ruby-on-rails - RSpec 中是否有与 Cucumber 的 "Scenarios"等效的内容,还是我以错误的方式使用 RSpec?

转载 作者:行者123 更新时间:2023-12-03 13:59:46 28 4
gpt4 key购买 nike

Cucumber's Scenarios 的简洁性和实用性给我留下了深刻的印象,它们是测试大量不同案例的好方法。

例如 cucumber 场景示例

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', nil, nil], :expected => false },
    { :inputs => ['1/1/2001', '1/1/2001', nil], :expected => true },
    { :inputs => ['1/1/2001', '1/2/2001', nil], :expected => true },
    { :inputs => ['1/1/2001', '1/1/2001', '1/2/2001'], :expected => false },
    { :inputs => ['1/1/2001', '1/1/2001', '1/1/3001'], :expected => true },
    { :inputs => ['1/1/2001', '1/1/3001', '1/2/3001'], :expected => false },
    { :inputs => ['1/1/3001', '1/1/3001', '1/2/3001'], :expected => false },
    { :inputs => ['1/1/2001', nil, '1/1/2001'], :expected => false },
    { :inputs => ['1/1/2001', nil, '1/1/3001'], :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", pre_order_on == "1/1/2001", for_sale_on == "1/2/2001"

    这是所有绿色的样子:
    ..........

    Finished in 0.3889 seconds
    10 examples, 0 failures

    关于ruby-on-rails - RSpec 中是否有与 Cucumber 的 "Scenarios"等效的内容,还是我以错误的方式使用 RSpec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679875/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com