黄瓜方案的简洁性和实用性给我留下了深刻的印象,它们是测试大量不同案例的好方法。
例如黄瓜方案示例
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的“方案”等效的东西吗,还是我使用RSpec的方式错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4679875/