简体   繁体   English

测试 RSpec - Output 活动消息到 Ruby 中的 STDOUT

[英]Testing with RSpec - Output activity messages to STDOUT in Ruby

I'm looking for some help outputting the activity messages into the command line window. I know this may seem backwards but this is the task I've been given.我正在寻找一些帮助将活动消息输出到命令行 window。我知道这可能看起来倒退,但这是我被赋予的任务。 I've already written tests so that they all pass but I need to convert the below activity into the command line window. It's just a game that resembles the Impossible Machine game.我已经编写了测试,以便它们全部通过,但我需要将以下活动转换为命令行 window。这只是一款类似于不可能的机器游戏的游戏。

Firstly I need to create a process which starts the Impossible Machine, then simulate each of the activities being initiated in succession before finishing.首先,我需要创建一个启动 Impossible Machine 的流程,然后在完成之前模拟连续启动的每个活动。

Of what I understand, all the messages displayed should be sent to the STDOUT channel.据我了解,所有显示的消息都应发送到 STDOUT 通道。 These are some of the tests that have been written:这些是已经编写的一些测试:

module ImpossibleMachine
# Input and output constants processed by subprocesses
DOWN_ARROW = 1
UP_ARROW = 2
RIGHT_ARROW = 3
REPEAT_ARROW = 4
END_PROCESS = 5 
START_CURRENT = 6

# RSpec Tests 
describe Game do
    describe "#start The impossible machine game" do
        before(:each) do
            @process = []
            @output = double('output').as_null_object
            @game = Game.new(@output)
        end
        it "sends a welcome message" do
            @output.should_receive(:puts).with('Welcome to the Impossible Machine!')
            @game.start
        end
        it "should contain a method created_by which returns the students name" do
            myname = @game.created_by
            myname.should == "My Name"
        end
        it "should perform lifts_lever_turns_wheel activity which returns REPEAT_ARROW" do
            @output.should_receive(:puts).with("Input: #{UP_ARROW}, Activity: Heave_ho_squeek_squeek")
            @process[1] = @game.lifts_lever_turns_wheel(UP_ARROW)
            @process[1].should == REPEAT_ARROW
        end
        it "sends a finishing message" do
            @output.should_receive(:puts).with('...Game finished.')
            @game.finish            
        end
    end
end

My only knowledge is that I need to start the module like this and then proceed to add code below it so that it outputs the activity messages to the command line:我唯一的知识是我需要像这样启动模块,然后继续在它下面添加代码,以便它将活动消息输出到命令行:

module ImpossibleMachine
@process = []
g = Game.new(STDOUT)

Hope that makes sense.希望这是有道理的。

It is not very clear from your question - you want the game to show its output to STDOUT when you run the rspec? 您的问题还不清楚-您想让游戏在运行rspec时将其输出显示到STDOUT吗?

If this is the case, I'll explain why in your code as you post it, it does not happen: 如果是这种情况,我将在发布代码时向您解释为什么不会发生这种情况:

When you create the new game @game you create it with Game.new(@output) . 创建新游戏@game ,可以使用Game.new(@output)创建它。 The @output is a double , which means that it is not really an output object at all, but it is a mock object instead. @outputdouble ,这意味着它根本不是一个输出对象,而是一个模拟对象。

This is totally fine, by the way. 顺便说一句,这完全没问题。 The only problem with it is that it doesn't actually print anything to the console. 与它唯一的问题是,它实际上并没有任何打印到控制台。

If you want to make the tests, while actually printing to the console, you should pass the actual STDOUT object: 如果要进行测试,则在实际打印到控制台时,应传递实际的STDOUT对象:

before(:each) do
  @process = []
  @output = STDOUT
  @game = Game.new(@output)
end

This will almost work, as it will print all messages except the ones you stub in your tests @output.should_receive(...) . 几乎可以用,因为它将打印测试@output.should_receive(...)存根之外的所有消息。 To make those work, you should add and_call_original to each expectation: 为了使这些起作用,您应该在每个期望中添加and_call_original

@output.should_receive(:puts).with('Welcome to the Impossible Machine!').and_call_original

You can do this without doubles:你可以在没有双打的情况下做到这一点:

  it "should perform lifts_lever_turns_wheel activity which returns REPEAT_ARROW" do
    expect(STDOUT).to receive(:puts).with("Input: #{UP_ARROW}, Activity: Heave_ho_squeek_squeek")
    @process[1] = @game.lifts_lever_turns_wheel(UP_ARROW)
    @process[1].should == REPEAT_ARROW
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM