Skip to content

Instantly share code, notes, and snippets.

@dyba
Created August 6, 2012 05:41
Show Gist options
  • Select an option

  • Save dyba/3271084 to your computer and use it in GitHub Desktop.

Select an option

Save dyba/3271084 to your computer and use it in GitHub Desktop.
Testing a class method
class Movie < ActiveRecord::Base
def self.all_ratings
# debugger
find_by_sql("SELECT DISTINCT rating FROM movies").map(&:rating)
end
end
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