2 Replies - 781 Views - Last Post: 04 June 2014 - 08:33 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

testing that a value is part of a collection

Posted 04 June 2014 - 11:23 AM

Here is the method I am testing:

def corner_tactic
    corner_scenario = [1,3,7,9]
    middle_space = board.board_spaces[5] 
    corner_spaces = scenario_spaces_analysis(corner_scenario)
    if corner_spaces.all? {|space| space == ' '} && (middle_space != " ")
      return corner_scenario.sample
    end
    false
end



I want to write a test that will pass if any of the 4 values in corner_scenario are returned. the commented section in the following code illustrates what I am trying to achieve. I cant find an rspec syntax that tests that an outputted value is included in a collection of viable options.

  context "#corner_tactic - true" do
    before do
        board.board_spaces = { 
          1 => " ",2 => " ",3 => " ",
          4 => " ",5 => "X",6 => " ",
          7 => " ",8 => " ",9 => " "
        }  
    end

    it "it occupies a random corner when all corners are empty and the middle is occupied" do
      expect(ai.middle_tactic).to be == #### 1 || 3 || 7 || 9
    end
  end
end


This post has been edited by heaphyg: 04 June 2014 - 11:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: testing that a value is part of a collection

#2 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: testing that a value is part of a collection

Posted 04 June 2014 - 12:43 PM

got it. expect([1,3,7,9}.include?((ai.middle_tactic))

CORRECTION: this doesn't seem to do what I want.

This post has been edited by heaphyg: 04 June 2014 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: testing that a value is part of a collection

Posted 04 June 2014 - 08:33 PM

View Postheaphyg, on 04 June 2014 - 01:23 PM, said:

Here is the method I am testing:

def corner_tactic
    corner_scenario = [1,3,7,9]
    middle_space = board.board_spaces[5] 
    corner_spaces = scenario_spaces_analysis(corner_scenario)
    if corner_spaces.all? {|space| space == ' '} && (middle_space != " ")
      return corner_scenario.sample
    end
    false
end



I want to write a test that will pass if any of the 4 values in corner_scenario are returned. the commented section in the following code illustrates what I am trying to achieve. I cant find an rspec syntax that tests that an outputted value is included in a collection of viable options.

  context "#corner_tactic - true" do
    before do
        board.board_spaces = { 
          1 => " ",2 => " ",3 => " ",
          4 => " ",5 => "X",6 => " ",
          7 => " ",8 => " ",9 => " "
        }  
    end

    it "it occupies a random corner when all corners are empty and the middle is occupied" do
      expect(ai.middle_tactic).to be == #### 1 || 3 || 7 || 9
    end
  end
end



You're doing your RSPEC quite oddly. Can I get an entire context on that?

Here's an example of how it should be formatted (psuedo of course):
describe "Object" do
  describe "#method" do
    context "when true" do
      it "returns true" do
        expect(Object.method).to eq(true)
      end
    end

    context "when false" do
      it "returns false" do
        expect(Object.method).to eq(false)
      end
    end
  end
end



Use your contexts for logical states of a method, and describes to describe the behavior of a method. What this will read out as is:
Object
  #method
    when true
      returns true
    when false
      returns false



Don't add the second it inside the message.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1