|
44 | 44 | end
|
45 | 45 |
|
46 | 46 | # ASSIGNMENT #1
|
| 47 | + |
47 | 48 | # Write a similar test to the one above, that uses a custom matcher
|
48 | 49 | # instead of <, >, =.
|
49 | 50 | matcher :be_between_zero_and_nine do
|
50 |
| - match { |number| number.between?(0, 9) } |
51 | 51 | end
|
52 | 52 |
|
53 | 53 | # remove the 'x' before running this test
|
|
68 | 68 | end
|
69 | 69 |
|
70 | 70 | # ASSIGNMENT #2
|
| 71 | + |
71 | 72 | # Create a new instance of NumberGame and write a test for when the @guess
|
72 | 73 | # does not equal @solution.
|
73 | 74 | context 'when user guess is not correct' do
|
|
80 | 81 | # The #player_input method is used in the game as an argument passed into the
|
81 | 82 | # verify_input method. The #player_input method is not tested because it is a
|
82 | 83 | # private method. In addition, it is unnecessary to test methods that only
|
83 |
| - # contain puts and/or gets. However, at the bottom of the answer file is an |
84 |
| - # example of how you would test the #player_input method if it were not |
85 |
| - # private. |
| 84 | + # contain puts and/or gets. |
86 | 85 |
|
87 | 86 | # Since we do not have to test #player_input, let's test #verify_input.
|
88 | 87 |
|
89 | 88 | describe '#verify_input' do
|
90 |
| - # Note: #verify_input is a recursive method that will repeat until |
91 |
| - # number.match?(/^[0-9]$/) is true. |
| 89 | + subject(:verify_game) { described_class.new } |
| 90 | + # Note: #verify_input will only return a value if it matches /^[0-9]$/ |
92 | 91 |
|
93 | 92 | context 'when given a valid input as argument' do
|
94 | 93 | it 'returns valid input' do
|
95 | 94 | user_input = '3'
|
96 |
| - verified_input = game.verify_input(user_input) |
| 95 | + verified_input = verify_game.verify_input(user_input) |
97 | 96 | expect(verified_input).to eq('3')
|
98 | 97 | end
|
99 | 98 | end
|
100 | 99 |
|
101 |
| - # In order to test #verify_input receiving an invalid input, we need to use |
102 |
| - # a method stub. In this example, the method stub will return the valid |
103 |
| - # input, 'number_input', which will be the final result of this test. |
104 |
| - # To stub this method, you 'allow' the test subject (game) to receive the |
105 |
| - # :method_name and to return a specific value. |
| 100 | + # ASSIGNMENT #3 |
| 101 | + |
| 102 | + # Write a test for the following context. |
| 103 | + context 'when given invalid input as argument' do |
| 104 | + xit 'returns nil' do |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe '#player_turn' do |
| 110 | + # In order to test the behavior of #player_turn, we need to use a method |
| 111 | + # stub for #player_input to return a valid_input ('3'). To stub a method, |
| 112 | + # we 'allow' the test subject (player_game) to receive the :method_name |
| 113 | + # and to return a specific value. |
106 | 114 | # https://relishapp.com/rspec/rspec-mocks/v/2-14/docs/method-stubs/allow-with-a-simple-return-value
|
107 | 115 | # http://testing-for-beginners.rubymonstas.org/test_doubles.html
|
108 | 116 |
|
109 |
| - context 'when given invalid input once before valid input' do |
110 |
| - letter_input = 'g' |
111 |
| - number_input = '5' |
| 117 | + subject(:player_game) { described_class.new } |
112 | 118 |
|
113 |
| - # When using the same 'Arrange' part of a test, you can utilize before |
114 |
| - # hooks to set up the test conditions. |
115 |
| - # https://relishapp.com/rspec/rspec-core/v/2-0/docs/hooks/before-and-after-hooks\ |
| 119 | + context 'when user input is valid' do |
| 120 | + # To test the behavior, we want to test that the loop stops before the |
| 121 | + # puts 'Input error!' line. In order to test that this method is not |
| 122 | + # called, we use a message expectation. |
| 123 | + # https://relishapp.com/rspec/rspec-mocks/docs |
116 | 124 |
|
117 |
| - before do |
118 |
| - # The stub below will return the number_input when called. |
119 |
| - allow(game).to receive(:player_input).and_return(number_input) |
| 125 | + it 'stops loop and does not display error message' do |
| 126 | + valid_input = '3' |
| 127 | + allow(player_game).to receive(:player_input).and_return(valid_input) |
| 128 | + # To use a message expectation, move 'Assert' before 'Act'. |
| 129 | + expect(player_game).not_to receive(:puts).with('Input error!') |
| 130 | + player_game.player_turn |
120 | 131 | end
|
| 132 | + end |
121 | 133 |
|
122 |
| - it 'loops once until it receives valid input' do |
123 |
| - # The stub below will remove the 'Input error!' from appearing in the |
124 |
| - # test output. |
125 |
| - allow(game).to receive(:puts) |
126 |
| - |
127 |
| - # This test starts with the invalid parameter (letter_input = 'g'). |
128 |
| - verified_input = game.verify_input(letter_input) |
| 134 | + context 'when user inputs an incorrect value once, then a valid input' do |
| 135 | + # A method stub can be called multiple times and return different values. |
| 136 | + # https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/returning-a-value |
| 137 | + # Create a stub method to receive :player_input and return the invalid |
| 138 | + # 'letter' input, then the 'valid_input' |
129 | 139 |
|
130 |
| - # The result of 'verified_input' is the valid parameter, because |
131 |
| - # of the player_input stub in the before hook (number_input = '5'). |
132 |
| - expect(verified_input).to eq('5') |
| 140 | + # As the 'Arrange' step for tests grows, you can use a before hook to |
| 141 | + # separate the test from the set-up. |
| 142 | + # https://relishapp.com/rspec/rspec-core/v/2-0/docs/hooks/before-and-after-hooks\ |
| 143 | + # https://www.tutorialspoint.com/rspec/rspec_hooks.htm |
| 144 | + before do |
| 145 | + letter = 'd' |
| 146 | + valid_input = '8' |
| 147 | + allow(player_game).to receive(:player_input).and_return(letter, valid_input) |
133 | 148 | end
|
134 | 149 |
|
135 |
| - it 'displays error message once' do |
136 |
| - # Due to the loop, we can test that the game received :puts with the |
137 |
| - # error message one time. In order to test if this method is called, |
138 |
| - # we use a message expectation. |
139 |
| - # https://relishapp.com/rspec/rspec-mocks/docs |
140 |
| - |
141 |
| - # To set a message expectation, move 'Assert' before 'Act'. |
142 |
| - expect(game).to receive(:puts).once.with('Input error!') |
143 |
| - game.verify_input(letter_input) |
| 150 | + # When using message expectations, you can specify how many times you |
| 151 | + # expect the message to be received. |
| 152 | + # https://relishapp.com/rspec/rspec-mocks/docs/setting-constraints/receive-counts |
| 153 | + it 'completes loop and displays error message once' do |
| 154 | + expect(player_game).to receive(:puts).with('Input error!').once |
| 155 | + player_game.player_turn |
144 | 156 | end
|
145 | 157 | end
|
146 | 158 |
|
147 |
| - # ASSIGNMENT #3 |
148 |
| - context 'when given invalid input twice before valid input' do |
149 |
| - letter_input = 'h' |
150 |
| - number_input = '3' |
151 |
| - # Create another invalid input (anything except a digit between 0 and 9). |
| 159 | + # ASSIGNMENT #4 |
152 | 160 |
|
| 161 | + # Write a test for the following context. |
| 162 | + context 'when user inputs two incorrect values, then a valid input' do |
153 | 163 | before do
|
154 |
| - # A method stub can be called multiple times and return different values. |
155 |
| - # https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/returning-a-value |
156 |
| - # Create a stub method to receive :player_input and return your invalid |
157 |
| - # input and the number_input. |
158 |
| - end |
159 |
| - |
160 |
| - # remove the 'x' before running this test |
161 |
| - xit 'loops twice until it receives valid input' do |
162 |
| - # Creating a stub method for :puts is optional |
163 |
| - |
164 |
| - verified_input = game.verify_input(letter_input) |
165 |
| - expect(verified_input).to eq(number_input) |
166 | 164 | end
|
167 | 165 |
|
168 |
| - # remove the 'x' before running this test |
169 |
| - xit 'displays error message twice' do |
| 166 | + xit 'completes loop and displays error message twice' do |
170 | 167 | end
|
171 | 168 | end
|
172 | 169 | end
|
|
192 | 189 | end
|
193 | 190 | end
|
194 | 191 |
|
195 |
| - # ASSIGNMENT #4 |
196 |
| - context 'when count is 2-3' do |
197 |
| - # Create a new instance of NumberGame, with specific values for @solution, |
198 |
| - # @guess, and @count. |
| 192 | + # ASSIGNMENT #5 |
199 | 193 |
|
| 194 | + # Create a new instance of NumberGame, with specific values for @solution, |
| 195 | + # @guess, and @count |
| 196 | + context 'when count is 2-3' do |
200 | 197 | # remove the 'x' before running this test
|
201 | 198 | xit 'outputs correct phrase' do
|
202 | 199 | congrats_phrase = "Congratulations! You picked the random number in 3 guesses!\n"
|
203 | 200 | expect { game.final_message }.to output(congrats_phrase).to_stdout
|
204 | 201 | end
|
205 | 202 | end
|
206 | 203 |
|
207 |
| - # ASSIGNMENT #5 |
208 |
| - # Create a new instance of NumberGame, with specific values for @solution, |
209 |
| - # @guess, and @count. Write a test for the following context. |
| 204 | + # ASSIGNMENT #6 |
| 205 | + |
| 206 | + # Write a test for the following context. |
210 | 207 | context 'when count is 4 and over' do
|
211 | 208 | # remove the 'x' before running this test
|
212 | 209 | xit 'outputs correct phrase' do
|
|
0 commit comments