黄瓜测试双:场景失败,但其步骤通过 [英] Cucumber test double: scenario failing but its steps passing

查看:210
本文介绍了黄瓜测试双:场景失败,但其步骤通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RSpec Book 学习Rspec + Cucumber。我只是在开始,而开发一个Codebreaker游戏。

I'm learning Rspec + Cucumber with The RSpec Book. I'm just at the beginning, while developing a Codebreaker game.

在它,有一个功能Codebreaker开始游戏,代表只是一个用户键入命令shell并得到两个响应:Welcome to Codebreaker!和输入猜测:。这里是功能的样子:

In it, there is a feature "Codebreaker starts game" that represents simply a user typing a command in the shell and getting two responses: "Welcome to Codebreaker!" and "Enter a guess:". Here it is how the feature looks like:

Feature: code-breaker starts game
   As a code-breaker
   I want to start a game
   So that I can break the code

   Scenario: start game
      Given I am not yet playing
      When I start a new game
      Then I should see "Welcome to Codebreaker!"
      And I should see "Enter a guess:"

cucumber 脚本,本书正在创建一个mock对象输出,期望接收 puts >输入一个猜测:参数。这里是它在步骤定义中的样子:

As the output is used by the cucumber script, the book is creating a mock object output which is expecting to receive the puts message with Welcome to Codebreaker! and Enter a guess: argument. Here it is how it looks in the step definitions:

#the mock object
class Output
   def messages
      @messages ||= []
   end

   def puts(message)
      messages << message
   end
end

def output
   @output ||= Output.new
end

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
   game = Codebreaker::Game.new(output)
   game.start
end

Then /^I should see "([^"]*)"$/ do |message|
   output.messages.should include(message)
end

好,到现在没有问题。

做这个练习,我记得以前读过rspeck doubles框架可以在黄瓜里面使用,所以我想我可以清理一下。

Doing this exercise, I remembered to had read before that rspeck doubles framework could be used inside cucumber, so I thought I could clean it a little bit.

首先,我在 support / env.rb 中加入了rspeck doubles框架:

First, I have included rspeck doubles framework in support/env.rb:

require 'cucumber/rspec/doubles'

然后我改变了步骤定义:

And then I have changed the step definitions:

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
   @output = double('output').as_null_object #the mock object
   game = Codebreaker::Game.new(@output)
   game.start
end

Then /^I should see "([^"]*)"$/ do |message|
   @output.should_receive(:puts).with(message)
end

奇怪的想法是,现在,当我执行黄瓜的功能,在摘要我得到所有4步骤传递,但不是整个方案。这怎么可能?它发生了什么?这里是我从命令行获得的输出:

The strange think is that now, when I execute the feature with cucumber, in the summary I get all 4 steps passing but not the whole scenario. How is it possible? What is it happening? Here it is the output I get from the command line:

Feature: code-breaker starts game
   As a code-breaker
   I want to start a game
   So that I can break the code

  Scenario: start game                          # features/codebreaker_starts_game.feature:6
    Given I am not yet playing                  # features/step_definitions/codebreaker_steps.rb:1
    When I start a new game                     # features/step_definitions/codebreaker_steps.rb:4
    Then I should see "Welcome to Codebreaker!" # features/step_definitions/codebreaker_steps.rb:10
    And I should see "Enter a guess:"           # features/step_definitions/codebreaker_steps.rb:10
      (Double "output").puts("Welcome to Codebreaker!")
          expected: 1 time
          received: 0 times (RSpec::Mocks::MockExpectationError)
      /home/a_user/www/codebreaker/features/step_definitions/codebreaker_steps.rb:11:in `block in <top (required)>'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/error_generator.rb:80:in `__raise'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/error_generator.rb:39:in `raise_expectation_error'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/message_expectation.rb:251:in `generate_error'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/message_expectation.rb:207:in `verify_messages_received'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/method_double.rb:117:in `block in verify'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/method_double.rb:117:in `each'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/method_double.rb:117:in `verify'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/proxy.rb:88:in `block in verify'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/proxy.rb:88:in `each'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/proxy.rb:88:in `verify'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/methods.rb:116:in `rspec_verify'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/space.rb:11:in `block in verify_all'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/space.rb:10:in `each'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks/space.rb:10:in `verify_all'
      /var/lib/gems/1.9.1/gems/rspec-mocks-2.10.1/lib/rspec/mocks.rb:19:in `verify'
      /var/lib/gems/1.9.1/gems/cucumber-1.1.9/lib/cucumber/rspec/doubles.rb:12:in `After'

Failing Scenarios:
cucumber features/codebreaker_starts_game.feature:6 # Scenario: start game

1 scenario (1 failed)
4 steps (4 passed)
0m0.009s


推荐答案

当你设置 should_receive 的期望时,在未来,应该调用指定的方法 - 以前发生的任何事情都被忽略了(否则应该是过去时的has_received或类似的东西)。

When you set an expectation like should_receive, you're specifying that a some point in the future the specified method should be called - anything that has happened previously is ignored (or else it would be should have_received or something like that in the past tense).

在您的代码中,您在然后步骤中设置期望,但是方法在您的即之前),所以在那一点没有预期设置。你的double被设置为允许任何方法被调用,所以你没有错误,但当spec检查结束时,是否所有的期望已经满足,它会说不,并引发异常

In your code you're setting up the expectation in your Then step but the method gets called in your When step (i.e. before), so at that point no expectation has been setup. Your double is setup to allow any method to be called, so you get no errors, but when spec checks at the end whether all expectations have been satisfied it will say no and raise an exception

这篇关于黄瓜测试双:场景失败,但其步骤通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆