Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Minitest & Rails: Total BFFs

Minitest & Rails: Total BFFs

Rails 4 is switching to MiniTest from Test::Unit. What does this mean for you? MiniTest is more than an exercise in minimalism, it’s a full-featured testing framework. Using MiniTest, we can change our approach to application design and build Rails apps that are more flexible, easier to refactor, and more maintainable.

We’ll get straight to the good stuff: How to test validations. When to stub and when to mock. How to use fixtures, and when you should skip them. Can your model tests be faster? What about controller tests? And why you’d choose it over RSpec.

Have you worked on apps where the tests are mocked to the hilt and impossible to change when refactoring? Or wasted time hunting through dozens of files to find all the downstream methods called from one controller action? Or simply thrown tests away because your team can no longer maintain them? Then this session is for you.

blowmage

May 13, 2013
Tweet

More Decks by blowmage

Other Decks in Technology

Transcript

  1. require "minitest/autorun" # class Pony # def friendship # "magic"

    # end # end class PonyTest < Minitest::Test end test/pony_test.rb
  2. require "minitest/autorun" # class Pony # def friendship # "magic"

    # end # end class PonyTest < Minitest::Test def test_friendship end end test/pony_test.rb
  3. require "minitest/autorun" # class Pony # def friendship # "magic"

    # end # end class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  4. require "minitest/autorun" # class Pony # def friendship # "magic"

    # end # end class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 49523 # Running: E Finished in 0.000875s, 1142.8571 runs/s, 0.0000 assertions/s. 1) Error: PonyTest#test_friendship: NameError: uninitialized constant PonyTest::Pony test/pony_test.rb:11:in `test_friendship' 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
  5. require "minitest/autorun" # class Pony # def friendship # "magic"

    # end # end class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 49523 # Running: E Finished in 0.000875s, 1142.8571 runs/s, 0.0000 assertions/s. 1) Error: PonyTest#test_friendship: NameError: uninitialized constant PonyTest::Pony test/pony_test.rb:11:in `test_friendship' 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
  6. require "minitest/autorun" class Pony # def friendship # "magic" #

    end end class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  7. require "minitest/autorun" class Pony # def friendship # "magic" #

    end end class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 58892 # Running: E Finished in 0.000793s, 1261.0340 runs/s, 0.0000 assertions/s. 1) Error: PonyTest#test_friendship: NoMethodError: undefined method `friendship' for #<Pony:0x007fda26909db0> test/pony_test.rb:11:in `test_friendship' 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
  8. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  9. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 17894 # Running: F Finished in 0.000805s, 1242.2360 runs/s, 1242.2360 assertions/s. 1) Failure: PonyTest#test_friendship [test/pony_test.rb:11]: Failed assertion, no message given. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
  10. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  11. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 3512 # Running: . Finished in 0.000799s, 1251.5645 runs/s, 1251.5645 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
  12. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 3512 # Running: . Finished in 0.000799s, 1251.5645 runs/s, 1251.5645 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
  13. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  14. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 17894 # Running: F Finished in 0.000805s, 1242.2360 runs/s, 1242.2360 assertions/s. 1) Failure: PonyTest#test_friendship [test/pony_test.rb:11]: Failed assertion, no message given. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
  15. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert(Pony.new.friendship == "magic") end end test/pony_test.rb
  16. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert((Pony.new.friendship == "magic"), "Friendship expected to be magic") end end test/pony_test.rb
  17. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert((Pony.new.friendship == "magic"), "Friendship expected to be magic") end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 28853 # Running: F Finished in 0.000787s, 1270.6480 runs/s, 1270.6480 assertions/s. 1) Failure: PonyTest#test_friendship [test/pony_test.rb:11]: Friendship expected to be magic 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
  18. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert_equal "magic", Pony.new.friendship end end test/pony_test.rb
  19. require "minitest/autorun" class Pony def friendship # "magic" end end

    class PonyTest < Minitest::Test def test_friendship assert_equal "magic", Pony.new.friendship end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 53489 # Running: F Finished in 0.028344s, 35.2808 runs/s, 35.2808 assertions/s. 1) Failure: PonyTest#test_friendship [test/pony_test.rb:11]: Expected: "magic" Actual: nil 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
  20. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert_equal "magic", Pony.new.friendship end end test/pony_test.rb
  21. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert_equal "magic", Pony.new.friendship end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 13305 # Running: . Finished in 0.000884s, 1131.2217 runs/s, 1131.2217 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
  22. require "minitest/autorun" class Pony def friendship "magic" end end class

    PonyTest < Minitest::Test def test_friendship assert_equal "magic", Pony.new.friendship end end test/pony_test.rb $ ruby test/pony_test.rb Run options: --seed 13305 # Running: . Finished in 0.000884s, 1131.2217 runs/s, 1131.2217 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
  23. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  24. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  25. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  26. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  27. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  28. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  29. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    ponies(:twilight) end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb
  30. require 'test_helper' class PonyTest < ActiveSupport::TestCase def twilight @twilight ||=

    ponies(:twilight) end def test_name assert_equal "Twilight Sparkle", twilight.name end def test_friendship assert_equal "magic", twilight.friendship end end app/models/pony.rb $ ruby test/pony_test.rb Run options: --seed 13305 # Running: . Finished in 0.000884s, 1131.2217 runs/s, 1131.2217 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
  31. test/controllers/cupcakes_test.rb require "test_helper" class CupcakesControllerTest < ActionController::TestCase def test_index get

    :index assert_response :success end def test_show get :show, id: cupcakes(:sprinkle) assert_response :success end end
  32. test/controllers/cupcakes_test.rb require "test_helper" class CupcakesControllerTest < ActionController::TestCase def test_index get

    :index assert_response :success end def test_show get :show, id: cupcakes(:sprinkle) assert_response :success end end $ rake test:controllers Run options: --seed 22699 # Running tests: .. Finished tests in 0.124348s, 16.0839 tests/s, 16.0839 assertions/s. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  33. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.name, image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.image_url, image_tag_html end end test/helpers/cupcakes_helper_test.rb
  34. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.name, image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.image_url, image_tag_html end end test/helpers/cupcakes_helper_test.rb $ rake test:helpers Run options: --seed 59089 # Running tests: .. Finished tests in 0.051314s, 38.9757 tests/s, 77.9514 assertions/s. 2 tests, 4 assertions, 0 failures, 0 errors, 0 skips
  35. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.name, image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.image_url, image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match Cupcake::DEFAULT_IMAGE_URL, image_tag_html end end test/helpers/cupcakes_helper_test.rb
  36. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.name, image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.image_url, image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match Cupcake::DEFAULT_IMAGE_URL, image_tag_html end end test/helpers/cupcakes_helper_test.rb $ rake test:helpers Run options: --seed 23417 # Running tests: .E. Finished tests in 0.050823s, 59.0284 tests/s, 78.7045 assertions/s. 1) Error: CupcakesHelperTest#test_blank_image_url: NameError: uninitialized constant Cupcake::DEFAULT_IMAGE_URL app/helpers/cupcakes_helper.rb:5:in `cupcake_image_tag' test/helpers/cupcakes_helper_test.rb:22:in `test_blank_image_url' 3 tests, 4 assertions, 0 failures, 1 errors, 0 skips
  37. class Cupcake < ActiveRecord::Base DEFAULT_IMAGE_URL = "/images/cupcake.png" has_many :favorites has_many

    :ponies, through: :favorites end app/models/cupcake.rb $ rake test:helpers Run options: --seed 52680 # Running tests: .F. Finished tests in 0.028242s, 106.2248 tests/s, 212.4495 assertions/s. 1) Failure: CupcakesHelperTest#test_blank_image_url [~/pinkie_shop/test/helpers/ cupcakes_helper_test.rb:23]: Expected /\/images\/cupcake\.png/ to match "<img alt=\"Icy McIcerson\" src= \"\" />". 3 tests, 6 assertions, 1 failures, 0 errors, 0 skips
  38. module CupcakesHelper def cupcake_image_tag cupcake url = cupcake.image_url url =

    Cupcake::DEFAULT_IMAGE_URL if url.blank? image_tag url, alt: cupcake.name end end app/helpers/
  39. module CupcakesHelper def cupcake_image_tag cupcake url = cupcake.image_url url =

    Cupcake::DEFAULT_IMAGE_URL if url.blank? image_tag url, alt: cupcake.name end end app/helpers/ $ rake test:helpers Run options: --seed 32226 # Running tests: ... Finished tests in 0.049226s, 60.9434 tests/s, 121.8868 assertions/s. 3 tests, 6 assertions, 0 failures, 0 errors, 0 skips
  40. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.name, image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match @cupcake.image_url, image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match Cupcake::DEFAULT_IMAGE_URL, image_tag_html end end test/helpers/cupcakes_helper_test.rb
  41. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match "alt=\"#{@cupcake.name}\"", image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{@cupcake.image_url}\"", image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{Cupcake::DEFAULT_IMAGE_URL}\"", image_tag_html end end test/helpers/cupcakes_helper_test.rb
  42. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match "alt=\"#{@cupcake.name}\"", image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{@cupcake.image_url}\"", image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{Cupcake::DEFAULT_IMAGE_URL}\"", image_tag_html end end test/helpers/cupcakes_helper_test.rb $ rake test:helpers Run options: --seed 63012 # Running tests: ... Finished tests in 0.048934s, 61.3071 tests/s, 122.6141 assertions/s. 3 tests, 6 assertions, 0 failures, 0 errors, 0 skips
  43. test/models/pony_test.rb require "test_helper" class PonyTest < ActiveSupport::TestCase def twilight_sparkle @twilight_sparkle

    ||= Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight_sparkle.name end def test_friendship assert_equal "magic", twilight_sparkle.friendship end end
  44. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' class

    ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  45. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/spec" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... extend Minitest::Spec::DSL register_spec_type(self) do |desc| desc < ActiveRecord::Base if desc.is_a?(Class) end end
  46. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/spec" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... extend Minitest::Spec::DSL register_spec_type(self) do |desc| desc < ActiveRecord::Base if desc.is_a?(Class) end end
  47. require "test_helper" class PonyTest < ActiveSupport::TestCase def twilight_sparkle @twilight_sparkle ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight_sparkle.name end def test_friendship assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  48. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight_sparkle.name end def test_friendship assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  49. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight_sparkle.name end def test_friendship assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  50. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end it "knows it's name" do assert_equal "Twilight Sparkle", twilight_sparkle.name end it "knows about friendship" do assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  51. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end it "knows it's name" do assert_equal "Twilight Sparkle", twilight_sparkle.name end it "knows about friendship" do assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  52. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  53. require "test_helper" describe Pony do def twilight_sparkle @twilight_sparkle ||= Pony.new

    name: "Twilight Sparkle" end it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  54. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  55. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  56. require "test_helper" class PonyTest < ActiveSupport::TestCase def twilight_sparkle @twilight_sparkle ||=

    Pony.new name: "Twilight Sparkle" end def test_name assert_equal "Twilight Sparkle", twilight_sparkle.name end def test_friendship assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  57. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  58. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  59. test/controllers/cupcakes_test.rb require "test_helper" class CupcakesControllerTest < ActionController::TestCase def test_index get

    :index assert_response :success end def test_show get :show, id: cupcakes(:sprinkle) assert_response :success end end
  60. test/controllers/cupcakes_test.rb require "test_helper" describe CupcakesController do it 'shows a list

    of cupcakes' do get :index assert_response :success end it 'shows a cupcake' do get :show, id: cupcakes(:sprinkle) assert_response :success end end
  61. require 'test_helper' class CupcakesHelperTest < ActionView::TestCase def setup @cupcake =

    Cupcake.new name: "Icy McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" end def test_alt_text image_tag_html = cupcake_image_tag @cupcake assert_match "alt=\"#{@cupcake.name}\"", image_tag_html end def test_image_url image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{@cupcake.image_url}\"", image_tag_html end def test_blank_image_url @cupcake.image_url = nil image_tag_html = cupcake_image_tag @cupcake assert_match "src=\"#{Cupcake::DEFAULT_IMAGE_URL}\"", image_tag_html end end test/helpers/cupcakes_helper_test.rb
  62. require 'test_helper' describe CupcakeHelper do let(:cupcake) { Cupcake.new name: "Icy

    McIcerson", image_url: "http://i.imgur.com/mbWH1gM.png" } let(:image_tag_html) { cupcake_image_tag cupcake } it "sets the alt text" do image_tag_html.must_match "alt=\"#{cupcake.name}\"" end it "uses the image_url" do image_tag_html.must_match "src=\"#{cupcake.image_url}\"" end it "uses a default image url if needed" do cupcake.image_url = nil image_tag_html.must_match "src=\"#{Cupcake::DEFAULT_IMAGE_URL}\"" end end test/helpers/cupcakes_helper_test.rb
  63. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  64. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb $ rake test:models Run options: --seed 43295 # Running tests: ... Finished tests in 0.057017s, 52.6159 tests/s, 70.1545 assertions/s. 3 tests, 4 assertions, 0 failures, 0 errors, 0 skips
  65. test/models/pony_test.rb require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name:

    "Twilight Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end context do it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end end
  66. test/models/pony_test.rb require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name:

    "Twilight Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end context do it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end end $ rake test:models rake aborted! undefined method `context' for #<Class:0x007fa4364ea070> ~/pinkie_shop/test/models/pony_test.rb:10:in `block in <top (required)>' ~/pinkie_shop/test/models/pony_test.rb:3:in `<top (required)>' Tasks: TOP => test:models (See full trace by running task with --trace) Run options: --seed 55683 # Running tests: .. Finished tests in 0.057922s, 34.5292 tests/s, 51.7938 assertions/s. 2 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  67. test/models/pony_test.rb require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name:

    "Twilight Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end describe :friendship do it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end end
  68. test/models/pony_test.rb require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name:

    "Twilight Sparkle" } it "knows it's name" do twilight_sparkle.name.must_equal "Twilight Sparkle" end describe :friendship do it "knows about friendship" do twilight_sparkle.friendship.must_equal "magic" end end end $ rake test:models Run options: --seed 31855 # Running tests: .. Finished tests in 0.076128s, 26.2715 tests/s, 39.4073 assertions/s. 2 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  69. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/spec" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... class << self remove_method :describe end if self.respond_to? :describe extend Minitest::Spec::DSL register_spec_type(self) do |desc| desc < ActiveRecord::Base if desc.is_a?(Class) end end
  70. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/spec" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... class << self remove_method :describe end if self.respond_to? :describe extend Minitest::Spec::DSL register_spec_type(self) do |desc| desc < ActiveRecord::Base if desc.is_a?(Class) end end $ rake test:models Run options: --seed 24 # Running tests: ... Finished tests in 0.054639s, 54.9058 tests/s, 73.2078 assertions/s. 3 tests, 4 assertions, 0 failures, 0 errors, 0 skips
  71. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/spec" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... class << self remove_method :describe end if self.respond_to? :describe extend Minitest::Spec::DSL register_spec_type(self) do |desc| desc < ActiveRecord::Base if desc.is_a?(Class) end end
  72. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/rails" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in... # # Note: You'll currently still have to declare fixtures explicitly... # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  73. test/pony_assertions.rb module PonyAssertions def assert_favorite cupcake, pony assert pony.cupcakes.include?(cupcake), "Expected

    #{pony.name} to have favorited #{cupcake.name}" end end class ActiveSupport::TestCase include PonyAssertions end
  74. test/pony_expectations.rb module PonyExpectations # assert_favorite apple_cupcake, apple_jack # apple_jack.must_have_favorited apple_cupcake

    infect_an_assertion :assert_favorite, :must_have_favorited end class Object include PonyExpectations end
  75. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/rails" require "pony_assertions" require "pony_expectations" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  76. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/rails" require "pony_assertions" require "pony_expectations" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  77. test/models/favorite_test.rb require "test_helper" describe Favorite do it "allows ponies to

    favorite cupcakes" do ponies(:twilight).must_have_favorited cupcakes(:sprinkle) end end
  78. require "test_helper" describe Pony do let(:twilight_sparkle) { Pony.new name: "Twilight

    Sparkle" } it "knows it's name" do assert_equal "Twilight Sparkle", twilight_sparkle.name end it "knows about friendship" do assert_equal "magic", twilight_sparkle.friendship end end test/models/pony_test.rb
  79. require "test_helper" class PonyTest < ActiveSupport::TestCase def twilight_sparkle @twilight_sparkle ||=

    Pony.new name: "Twilight Sparkle" end def test_name twilight_sparkle.name.must_equal "Twilight Sparkle" end def test_friendship twilight_sparkle.friendship.must_equal "magic" end end test/models/pony_test.rb
  80. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/rails" require "minitest/rails/capybara" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  81. test/test_helper.rb ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require

    "minitest/rails" require "minitest/rails/capybara" class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end
  82. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup @pinkie_password

    = "pinkiepie" @pinkie = ponies :pinkie @pinkie.password = @pinkie_password @pinkie.save end end
  83. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup @pinkie_password

    = "pinkiepie" @pinkie = ponies :pinkie @pinkie.password = @pinkie_password @pinkie.save end end
  84. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path assert_have_content page, "login" assert_have_content page, "signup" refute_have_content page, "logout" end end $ rake minitest:features Run options: --seed 65422 # Running tests: . Finished tests in 0.179815s, 5.5613 tests/s, 16.6838 assertions/s. 1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  85. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path assert_have_content page, "login" assert_have_content page, "signup" refute_have_content page, "logout" end end $ rake minitest:features Run options: --seed 65422 # Running tests: . Finished tests in 0.179815s, 5.5613 tests/s, 16.6838 assertions/s. 1 tests, 3 assertions, 0 failures, 0 errors, 0 skips $ rake minitest:features Run options: --seed 65422 # Running tests: . Finished tests in 0.179815s, 5.5613 tests/s, 16.6838 assertions/s. 1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  86. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path assert_have_content page, "login" assert_have_content page, "signup" refute_have_content page, "logout" end end $ rake minitest:features Run options: --seed 65422 # Running tests: . Finished tests in 0.179815s, 5.5613 tests/s, 16.6838 assertions/s. 1 tests, 3 assertions, 0 failures, 0 errors, 0 skips $ rake minitest:features Run options: --seed 65422 # Running tests: . Finished tests in 0.179815s, 5.5613 tests/s, 16.6838 assertions/s. 1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  87. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path assert_have_content page, "login" assert_have_content page, "signup" refute_have_content page, "logout" click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_have_content page, "logout" assert_have_content page, @pinkie.email refute_have_content page, "login" refute_have_content page, "signup" end end
  88. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path assert_have_content page, "login" assert_have_content page, "signup" refute_have_content page, "logout" click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_have_content page, "logout" assert_have_content page, @pinkie.email refute_have_content page, "login" refute_have_content page, "signup" end end $ rake minitest:features Run options: --seed 27176 # Running tests: . Finished tests in 0.318181s, 3.1429 tests/s, 22.0001 assertions/s. 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  89. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    end def test_login_as_pinkie_pie ... end def assert_nav_links_logged_in pony page.must_have_content "logout" page.must_have_content pony.email page.wont_have_content "login" page.wont_have_content "signup" end def refute_nav_links_logged_in pony = nil page.must_have_content "login" page.must_have_content "signup" page.wont_have_content "logout" end end
  90. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    end def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  91. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    end def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end $ rake minitest:features Run options: --seed 51819 # Running tests: . Finished tests in 0.285183s, 3.5065 tests/s, 24.5456 assertions/s. 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  92. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    end def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end $ rake minitest:features Run options: --seed 51819 # Running tests: . Finished tests in 0.285183s, 3.5065 tests/s, 24.5456 assertions/s. 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  93. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  94. test/features/login_test.rb require "test_helper" class LoginTest < Capybara::Rails::TestCase def setup ...

    def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  95. test/features/login_test.rb require "test_helper" feature "Login Feature" do def setup ...

    def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  96. test/features/login_test.rb require "test_helper" feature "Login Feature" do def setup ...

    def test_login_as_pinkie_pie visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  97. test/features/login_test.rb require "test_helper" feature "Login Feature" do def setup ...

    scenario "logging in as Pinkie Pie" do visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: @pinkie.email fill_in "Password", with: @pinkie_password click_button "Sign in" assert_nav_links_logged_in @pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  98. test/features/login_test.rb require "test_helper" feature "Login Feature" do def setup @pinkie_password

    = "pinkiepie" @pinkie = ponies :pinkie @pinkie.password = @pinkie_password @pinkie.save end scenario "logging in as Pinkie Pie" do ... def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  99. test/features/login_test.rb require "test_helper" feature "Login Feature" do given(:pinkie_password) { "pinkiepie"

    } def setup @pinkie = ponies :pinkie @pinkie.password = pinkie_password @pinkie.save end scenario "logging in as Pinkie Pie" do ... def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  100. test/features/login_test.rb require "test_helper" feature "Login Feature" do given(:pinkie_password) { "pinkiepie"

    } given(:pinkie) { p = ponies :pinkie p.password = pinkie_password p.save p } scenario "logging in as Pinkie Pie" do ... def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  101. test/features/login_test.rb require "test_helper" feature "Login Feature" do given(:pinkie_password) { "pinkiepie"

    } given(:pinkie) { p = ponies :pinkie ... } scenario "logging in as Pinkie Pie" do visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: pinkie.email fill_in "Password", with: pinkie_password click_button "Sign in" assert_nav_links_logged_in pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end
  102. test/features/login_test.rb require "test_helper" feature "Login Feature" do given(:pinkie_password) { "pinkiepie"

    } given(:pinkie) { p = ponies :pinkie ... } scenario "logging in as Pinkie Pie" do visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: pinkie.email fill_in "Password", with: pinkie_password click_button "Sign in" assert_nav_links_logged_in pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end $ rake minitest:features Run options: --seed 51819 # Running tests: . Finished tests in 0.285183s, 3.5065 tests/s, 24.5456 assertions/s. 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  103. test/features/login_test.rb require "test_helper" feature "Login Feature" do given(:pinkie_password) { "pinkiepie"

    } given(:pinkie) { p = ponies :pinkie ... } scenario "logging in as Pinkie Pie" do visit root_path refute_nav_links_logged_in click_link "login" fill_in "Email", with: pinkie.email fill_in "Password", with: pinkie_password click_button "Sign in" assert_nav_links_logged_in pinkie end def assert_nav_links_logged_in pony ... def refute_nav_links_logged_in pony = nil ... end $ rake minitest:features Run options: --seed 51819 # Running tests: . Finished tests in 0.285183s, 3.5065 tests/s, 24.5456 assertions/s. 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  104. Twilight Sparkle Image Credits • Twilight Sparkle is Curious by

    Alixey http://alixey.deviantart.com/art/Twilight-Sparkle-is-Curious-335163906 • Skeptical, annoyed Twilight by MrHavre http://mrhavre.deviantart.com/art/Skeptical-annoyed-Twilight-333256895 • Twilight memories by HankOfficer http://hankofficer.deviantart.com/art/Twilight-memories-316846734 • Twilight Sparkle's Transformation by Sairoch http://sairoch.deviantart.com/art/Twilight-Sparkle-s-Transformation-356096575 • Twilight Sparkle Alicorn Vector by Kamyk962 http://kamyk962.deviantart.com/art/Twilight-Sparkle-Alicorn-Vector-355000137
  105. Pinkie Pie Image Credits •Pinkie Pie by KyssS by KyssS90

    http://kysss90.deviantart.com/art/Pinkie-Pie-by-KyssS-353498032 •Sly Pinkie Pie by Sairoch http://sairoch.deviantart.com/art/Sly-Pinkie-Pie-353454194 •Season 2 Poster Pinkie Pie by VladimirMacHolzraum http://vladimirmacholzraum.deviantart.com/art/Season-2-Poster- Pinkie-Pie-333531305 •Pinkie Pie by CrusierPL http://crusierpl.deviantart.com/art/Pinkie-Pie-279503261
  106. Pinkie Pie Image Credits •Pinkie Glasses by J-Brony http://j-brony.deviantart.com/art/Pinkie-Glasses-264170145 •Extremely

    Surprised Pinkie by IamthegreatLyra http://iamthegreatlyra.deviantart.com/art/Extremely-Surprised- Pinkie-279356130 •Haters Gonna' Hate by binaryNinj4 http://binaryninj4.deviantart.com/art/Haters-Gonna-Hate-201850847 •Pinkie Pie Overwrought by Sairoch http://sairoch.deviantart.com/art/Pinkie-Pie-Overwrought-363028991