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 8735515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:04:41+00:00 2026-06-13T10:04:41+00:00

I have this method as part of a larger class. I’m trying to write

  • 0

I have this method as part of a larger class. I’m trying to write a test for it but I’m new to rspec and I’m kinda stumped…I can test ‘drawgrid’ if I comment out everything in the 9.times loop. but if I uncomment that code the current test fails. I need to test that the play method…runs the game. that it puts ‘drawgrid’…runs the game sequence 9 times putting the ‘drawgrid’ after each turn. But I’m not sure how to do this. Any pointers are greatly appreciated.

Below is the play method and it’s current spec

      def play
        #draw the board
        puts drawgrid

        #make a move
        turn = 0

        9.times do

          if turn.even?

            @player = @player_h.move_human("X", @board)

            @move = @player.to_sym
            @marker = @player_h.boardpiece

            does_move_exist(@move,@marker)
            is_a_human_win(@board)

          else

            @player = @player_c.move_computer("O", @board)

            @move = @player
            @marker = @player_c.boardpiece

            does_move_exist(@move,@marker)
            is_a_computer_win(@board)

          end

          puts drawgrid

          turn += 1
        end # 9.times ends
      end

current spec….

      describe 'play method' do
        it 'draws the game grid' do
          @player_human = Player.new('X')
          @player_computer = Player.new('O')
          @board = Board.new
          @game = Game.new(@player_human, @player_computer, @board)

          @game.should_receive(:puts).with("\na  | |  \n----------\nb  | |  \n----------\nc  | |  \n----------\n  1 2 3\n")

          @game.play
        end
      end
      describe '9.times' do
        it 'runs game sequence 9 times...once per board spot' do
          @player_human2 = Player.new('X')
          @player_computer2 = Player.new('O')
          @board2 = Board.new
          @game2 = Game.new(@player_human2, @player_computer2, @board2)

          turn = 0       
          9.times do
            if turn.even?
              @player_human2.should_receive(:puts).with("human move...")
              @player_human2.stub(:gets).and_return("b2")
            else
              @player_human2.should_receive(:puts).with("computer move...")
              @player_human2.stub(:gets).and_return("a1")
            end
            turn += 1
          end
        @game2.play
        end
      end
  • 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-06-13T10:04:42+00:00Added an answer on June 13, 2026 at 10:04 am

    In general, I feel like both your code and your test are trying to do too much in one method. The interesting bit about your play method isn’t so much the 9 times as what happens inside of that loop. My first suggestion for refactoring that is to turn what’s inside that loop into a method called “take_turn” or something similar.

    Then you could write specs for what happens for a single turn. And, your spec for the play method would test that the take_turn method is called 9 times.

    That’s not to say that you couldn’t keep your code the way that it is and write an effective test for it… you just can’t be super surgical about what you’re testing.

    Hope that helps.

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

Sidebar

Related Questions

I have a web method like this as part of a web service public
I have this method that changes an larger image source on click. I need
I have this method call I have to use... financial_document.assets.length But financial_document.assets could be
This is part of a larger statement, but I'm wondering if CTE or another
I have this method which lets me translate the position of an object, animating
I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have this method which leaks ~ 6KB : + (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
I have this method (someone else wrote it!) - (CGPDFDocumentRef)getPdf { NSArray *paths =
I have this method that gets called when i press a button and removes
I have this method for grabbing the file name from a string URI. What

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.