在RSpec中是否有对应于Cucumber的“Scenarios”的等价物。还是我用RSpec的方式错了? [英] Is there an equivalent in RSpec to Cucumber's "Scenarios" or am I using RSpec the wrong way?
问题描述
我对黄瓜方案的简洁性和有用性印象深刻,它们是测试不同案例的负载的好方法。
I'm impressed at the brevity and usefulness of Cucumber's Scenarios, they're a great way to test a load of different cases.
例如。 示例黄瓜方案
e.g. example Cucumber scenario
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的更多关于黄瓜屏幕录像)
RSpec有等效吗? / strong>
Is there an equivalent in RSpec?
我想在RSpec中做同样的事情,并通过在方案表中将不同的测试降低到一行来干掉我的代码。
I'd like to do the same thing in RSpec and to DRY up my code by reducing different tests down to a row in a scenario table.
虽然我可以自己编写代码来做这件事,但我考虑它的事实让我想到两件事:
Although I could write the code to do this myself, the fact that I'm considering it makes me wonder two things
-
如果这很有用,它可能已经存在,如何使用它?
If this is useful it probably already exists in which case how do I use it?
它不存在,它建议它不应该这样做,并且我接近的问题不正确,我应该如何重新思考我的方法RSpec?
If it doesn't exist it suggests it shouldn't be done this way and that I'm approaching the problem incorrectly, how should I rethink my approach to RSpec?
哪个答案是正确的,如果它是有用的,我该怎么办?
Which answer is right and if it is useful, how do I do it?
推荐答案
p>尝试以下方法。我喜欢它的结果。
Try the following approach. I like the way it turned out.
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
两个世界,因为它让我不再重复自己,并为每个条件创建一个不同的测试。
I think this gives the best of both worlds, because it keeps me from repeating myself, and it creates a different test for each condition.
这是失败的样子:
....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"
以下是所有绿色的样子:
And here's what all green looks like:
..........
Finished in 0.3889 seconds
10 examples, 0 failures
这篇关于在RSpec中是否有对应于Cucumber的“Scenarios”的等价物。还是我用RSpec的方式错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!