Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created July 6, 2009 22:47
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 nicholasjhenry/141740 to your computer and use it in GitHub Desktop.
Save nicholasjhenry/141740 to your computer and use it in GitHub Desktop.
require 'test_helper'
require 'mocha'
class StatTest < ActiveSupport::TestCase
test "should belong to user" do
assert_kind_of User, stats(:one).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 aspect when validating with a different aspect" do
stat_1 = build_stat(:aspect => "test_1")
stat_1.save!
stat_2 = build_stat(:aspect => "test_2")
stat_2.valid?
assert_nil stat_2.errors.on(:aspect)
end
test "should add error on aspect when validating with the same aspect" do
stat_1 = build_stat(:aspect => "test")
stat_1.save!
stat_2 = build_stat(:aspect => "test")
stat_2.valid?
assert_not_nil stat_2.errors.on(:aspect)
end
test "should not add error on aspect when validating an updated stat" do
stat = build_stat
stat.save!
stat.third_field = "test"
stat.valid?
assert_nil stat.errors.on(:aspect)
end
test "should aspect be read only" do
stat = build_stat(:aspect => "test")
stat.save!
stat.update_attribute(:aspect, "changed")
stat.reload
assert_equal "test", stat.aspect
end
test "should user be read only" do
stat = build_stat(:user => users(:bob))
stat.save!
stat.update_attribute(:user, users(:john))
stat.reload
assert_equal users(:bob), stat.user
end
private
def build_stat(attributes={})
Stat.new({:user => users(:bob), :aspect => "my aspect"}.
merge(attributes))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment