Created
August 6, 2012 05:41
-
-
Save dyba/3271084 to your computer and use it in GitHub Desktop.
Testing a class method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Movie < ActiveRecord::Base | |
| def self.all_ratings | |
| # debugger | |
| find_by_sql("SELECT DISTINCT rating FROM movies").map(&:rating) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'minitest/autorun' | |
| require 'ostruct' | |
| require 'debugger' | |
| require_relative '../minitest_helper_lite' | |
| stub_module 'ActiveRecord::Base' | |
| require_relative '../../app/models/movie' | |
| describe Movie do | |
| before do | |
| @it = Movie.new | |
| end | |
| describe "#all_ratings" do | |
| it "returns all unique MPAA movie ratings" do | |
| stub(@it).rating { '' } | |
| stub(Movie).find_by_sql("SELECT DISTINCT rating FROM movies") do | |
| ['G', 'PG-13', 'R', 'R', 'G', 'PG'].map! do |r| | |
| stub(Movie.new).rating { r } | |
| end | |
| end | |
| ['G', 'PG-13', 'R', 'PG'].each do |rating| | |
| Movie.all_ratings.must_include rating | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment