Wayne Rumble Wayne Rumble - 7 months ago 11
Ruby Question

How should I test this method using RSpec?

I am new to rspec and ruby and am having trouble writing a test for the following method using doubles etc.

def choices player1_choice, player2_choice
@choices_array = [player1_choice, player2_choice]
end


I have tried several ways but can't get it right. Any help would be much appreciated.

Answer

Here is a basic unit test for this method. You would not need to use a double for this method.

describe YourClassName do

  context "#choices" do

    it "should return an array with 2 player choices" do
      player1_choice = "a"
      player2_choice = "b"
      output = choices(player1_choice, player2_choice)
      expect(output.count).to eq(2)
      expect(output[0]).to eq(player1_choice)
      expect(output[1]).to eq(player2_choice)
    end

  end

end

Doubles/stubs are usually used around methods/objects that contain complex operations such as a network connection, integrating with a 3rd party library without implementation details, etc.

Comments