Permalink
Browse files

added mastermind/play view logic and spec

  • Loading branch information...
1 parent eecb559 commit 07f3d567f70aa525facba9ea2f310370c12d1cb1 @avalanche123 committed May 23, 2011
Showing with 76 additions and 1 deletion.
  1. +19 −0 app/views/mastermind/play.html.erb
  2. +57 −1 spec/views/mastermind/play.html.erb_spec.rb
@@ -0,0 +1,19 @@
+<h1>Mastermind</h1>
+<% if @game.finished? %>
+ <% if @game.won? %>
+ <p>Congratulations, you won!</p>
+ <% else %>
+ <p>You lost, the code was <%= @game.code %></p>
+ <% end %>
+ <p><%= link_to('Play again?', play_path) %></p>
+<% else %>
+ <% if @guess %>
+ <p>You guessed <%= @guess.matches %> pegs</p>
+ <% end %>
+ <p>Enter your guess:</p>
+
+ <%= form_tag('/guess', :method => "post") do %>
+ <%= guess_input_tag('guess', Rails.application.config.pegs, Rails.application.config.colors) %>
+ <%= submit_tag("Guess") %>
+ <% end %>
+<% end %>
@@ -1,5 +1,61 @@
require 'spec_helper'
describe "mastermind/play.html.erb" do
- pending "add some examples to (or delete) #{__FILE__}"
+ context "when the game is not finished" do
+ before do
+ assign(:game, Game.new(:finished => false))
+ end
+
+ it "lets user submit a guess" do
+ render
+ rendered.should contain('Enter your guess:')
+ end
+
+ context "when user submitted a guess" do
+ before do
+ assign(:guess, Guess.new(:matches => 2))
+ end
+
+ it "displays number of matches" do
+ render
+ rendered.should contain('You guessed 2 pegs')
+ end
+ end
+ end
+
+ context "when the game is finished" do
+ let(:game) { Game.new(:finished => true) }
+
+ before do
+ assign(:game, game)
+ end
+
+ context "when user won the game" do
+ before do
+ game.won = true
+ end
+
+ it "acknowledges vistory" do
+ render
+ rendered.should contain('Congratulations, you won!')
+ end
+ end
+
+ context "when user lost the game" do
+ before do
+ game.won = false
+ end
+
+ it "acknowledges loss" do
+ game.code = '1001'
+ render
+ rendered.should contain('You lost, the code was 1001')
+ end
+ end
+
+ it 'displays the "play again?" link' do
+ render
+ rendered.should contain('Play again?')
+ end
+ end
end

0 comments on commit 07f3d56

Please sign in to comment.