Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:46:10+00:00 2026-05-27T21:46:10+00:00

This question is related to the codebreaker example found in the RSpec book. See

  • 0

This question is related to the codebreaker example found in the RSpec book.
See code here: https://github.com/kristianmandrup/rspec-book-codebreaker/tree/master/features

I was wondering if anyone has made a full solution for the codebreaker game.

I am keen to see how to test the outer loop, which decides if the game should continue or not.

The functionality may look like this

  • You found the secret code!
  • Would you like to try again? : n
  • Good-bye

If the user selects ‘y’ then the game starts again.
I am interested to see the scenarios and step definitions.
Would it be something like:

Scenario: user finds secret code
Given the secret code is "1234" 
When I guess "1234" 
Then I should see "You found the secret code!" 
And I should see "Would you like to try again?"

Another spec file:

Background: Found secret code
Given the secret code is "1234" 
And I guess "1234" 

Scenario: user chooses to quit the game
Given I see "Would you like to try again?" 
When I enter "n" 
Then I should see "Good-bye" 

Scenario: user chooses to continue with the game
Given I see "Would you like to try again?" 
When I enter "y" 
Then I should see "Enter guess:"

As seen in the link above, currently a double (mock) output object is passed in. All output is captured by this double and then is used in assertions. Input is a bit different. When the application displays “Would you like to try again?”, then the input mock needs reply with ‘y’ or ‘n’, depending on the test. Perhaps this is not the way to go about it?

There is something similar to mimic a user guessing, that step definition looks like this:

When /^I guess "([^\"]*)"$/ do |guess|
@game.guess(guess)
end

This is the current main method:

def generate_secret_code
    options = %w[1 2 3 4 5 6]
    (1..4).map { options.delete_at(rand(options.length))}.join
end
game = Codebreaker::Game.new(STDOUT)
secret_code = generate_secret_code
at_exit { puts "\n***\nThe secret code was: #{secret_code}\n***" }

game.start(secret_code)
while guess = gets.chomp
    game.guess(guess)
end

The outer loop was added without any tests. I would like to know how to deal with the gets.chomp – how can I ‘mock’ that behaviour? Is this how to handle interactive input form the console?

I am actually using SpecFlow and C#, if possible provide a solution for C#.
A Ruby solution may also help…

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:46:11+00:00Added an answer on May 27, 2026 at 9:46 pm

    It is possible to launch a console application and attach to its standard input and output streams. See the example in the documentation of ProcessStartInfo.RedirectStandardInput.

    A cleaner way (which would also make the tests faster, but wouldn’t qualify as full end-to-end system testing) would be to restructure the application somewhat, so that it uses regular Stream objects instead of Console.In and Console.Out; the main() method can pass these classes to the rest of the application. Then, your tests can invoke the other methods directly and pass MemoryStream objects as the input/output streams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to another I ask recently, it can be found here
This question is related to this one: https://stackoverflow.com/questions/7906356/how-to-solve-a-a-reference-tracked-object-changed-reference-during-deserializat Since the above post has been
This question is related to a previous post of mine Here . Basically, I
This question is related to the question posted here: Why isn't my custom WCF
This question is related to: Casting items of a collection with code generation Since
This question is related to this post but I don't see how I can
This question is related to this one . Given this code: char *p =
This question is related to this Say if I get the below ajax code,
This question is related to this question. The following code compiles fine VC9 compiler
This question is related to (but perhaps not quite the same as): Does Django

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.