Skip to content

Instantly share code, notes, and snippets.

@GitNow
Created July 17, 2009 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitNow/4c009e230862d3a2e58a to your computer and use it in GitHub Desktop.
Save GitNow/4c009e230862d3a2e58a to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../test_helper'
require 'mocha'
class StatTest < ActiveSupport::TestCase
test "should belong to user" do
assert_kind_of User, stats(:user_1).user
end
test "should add error on user when validating with nil user" do
stat = build_stat(:user => nil)
stat.valid?
assert_not_nil stat.errors.on(:user_id)
end
test "should add error on user when validating with invalid user" do
stat = build_stat
User.any_instance.stubs(:valid?).returns(false)
stat.valid?
assert_not_nil stat.errors.on(:user)
end
test "should not add error on score_aspect when validating with a different score_aspect" do
stat_1 = build_stat(:score_aspect => "aspect_B")
stat_1.save!
stat_2 = build_stat(:score_aspect => "aspect_A")
stat_2.valid?
assert_nil stat_2.errors.on(:score_aspect)
end
test "should add error on score_aspect when validating with the same score_aspect" do
stat_1 = build_stat(:score_aspect => "aspect_A")
stat_1.save!
stat_2 = build_stat(:score_aspect => "aspect_A")
stat_2.valid?
assert_not_nil stat_2.errors.on(:score_aspect)
end
test "should not add error on score_aspect when validating an updated stat" do
stat = build_stat
stat.save!
stat.total_points = 50
stat.valid?
assert_nil stat.errors.on(:score_aspect)
end
test "should score_aspect be read only" do
stat = build_stat(:score_aspect => "aspect_A")
stat.save!
stat.update_attribute(:score_aspect, "aspect_B")
stat.reload
assert_equal "aspect_A", stat.score_aspect
end
test "should user be read only" do
stat = build_stat(:user => users(:user_1))
stat.save!
stat.update_attribute(:user, users(:user_2))
stat.reload
assert_equal users(:user_1), stat.user
end
private
def build_stat(attributes={})
Stat.new({:user => users(:user_1), :score_aspect => "aspect_A"}.
merge(attributes))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment