Hey everyone. I’ve been working on the ruby connect four game and I’ve run into an issue with my tests that I haven’t been able to find much help with on the internet. Here is a link to the question I have posted on stack overflow. Any feedback would be much appreciated. Thanks.
I believe it is because you have used StringIO which creates an IO stream. If you refer to the documentation of IO you will see
gets obtains the next string from the stream and then becomes nil which is why on second pass nil is returned from input.gets so you can’t call chomp on it.
I guess your best bet may be to use a mock object for input which can just keep returning a value. The benefit here will be you can set them to return a series of outputs so you could have them set an invalid output and then a valid one otherwise you have an issue where the test will loop forever as it keeps getting invalid attributes.
Have a google for mocks and stubs.
After much more googling, and many more tutorials and tests, I was able to solve my problem using your suggestion! Thanks for your advice. I was confused about mocks and stubs until I found some more references that made it clear to me. The amount of posts that use legacy rspec code also mixed me up. For those coming to this post with the same problem, try these resources:
Testing rspec IO - uses legacy syntax