ruby-on-rails – 在RSpec中有一个等同于Cucumber的“场景”,还是使用RSpec错误的方式?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在RSpec中有一个等同于Cucumber的“场景”,还是使用RSpec错误的方式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于黄瓜情景的简洁和实用,我印象深刻,它们是测试不同情况的好方法.

例如例如黄瓜情景

  1. Feature: Manage Users
  2. In order to manage user details
  3. As a security enthusiast
  4. I want to edit user profiles only when authorized
  5.  
  6. Scenario Outline: Show or hide edit profile link
  7. Given the following user records
  8. | username | password | admin |
  9. | bob | secret | false |
  10. | admin | secret | true |
  11. Given I am logged in as "<login>" with password "secret"
  12. When I visit profile for "<profile>"
  13. Then I should <action>
  14.  
  15. Examples:
  16. | login | profile | action |
  17. | admin | bob | see "Edit Profile" |
  18. | bob | bob | see "Edit Profile" |
  19. | | bob | not see "Edit Profile" |
  20. | bob | admin | not see "Edit Profile" |

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

RSpec有相当的等级吗?

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

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

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

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

解决方法

尝试以下方法.我喜欢原来的方式.
  1. describe StateDateMethods do
  2. before :each do
  3. @product = OpenStruct.new
  4. @product.extend StateDateMethods
  5. end
  6.  
  7. def parse_date(unparsed_date_value)
  8. unless unparsed_date_value.nil?
  9. DateTime.strptime(unparsed_date_value,'%m/%d/%Y')
  10. end
  11. end
  12.  
  13. context '#pre_order?' do
  14. examples = [
  15. # [visible_on,pre_order_on,for_sale_on] => method_result
  16. { :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 }
  17. ]
  18. examples.each do |example|
  19. inputs = example[:inputs]
  20.  
  21. 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
  22. @product.visible_on = parse_date(inputs[0])
  23. @product.pre_order_on = parse_date(inputs[1])
  24. @product.for_sale_on = parse_date(inputs[2])
  25.  
  26. @product.pre_order?.should == example[:expected]
  27. end
  28. end
  29. end
  30. end

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

这是一个失败的样子:

  1. ....F.....
  2.  
  3. Failures:
  4.  
  5. 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"
  6. Failure/Error: @product.pre_order?.should == example[:expected]
  7. expected: false
  8. got: true (using ==)
  9. # ./spec_no_rails/state_date_methods_spec.rb:40:in `block (4 levels) in <top (required)>'
  10.  
  11. Finished in 0.38933 seconds
  12. 10 examples,1 failure
  13.  
  14. Failed examples:
  15.  
  16. 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"

这就是所有绿色的样子:

  1. ..........
  2.  
  3. Finished in 0.3889 seconds
  4. 10 examples,0 failures

猜你在找的Ruby相关文章