Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Testing Primer (URUG presentation 7/12/2011)
class List < ActiveRecord::Base
belongs_to :user
validates_presence_of :name
validates_presence_of :user
end
require 'test_helper'
class ListTest < ActiveSupport::TestCase
setup do
@valid_params = { :name => "Mike Moore",
:user => User.new }
end
test "is valid with valid params" do
list = List.new @valid_params
assert list.valid?
end
test "needs a name to be valid" do
params = @valid_params.clone
params.delete :name
list = List.new params
refute list.valid?
assert list.errors[:name].include?("can't be blank")
end
test "needs a user to be valid" do
params = @valid_params.clone
params.delete :user
list = List.new params
refute list.valid?
assert list.errors[:user].include?("can't be blank")
end
end
class Task < ActiveRecord::Base
belongs_to :list
end
require 'test_helper'
class TaskTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
class User < ActiveRecord::Base
has_many :lists
def list_names
lists.map &:name
end
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
setup do
@user = User.new
end
test "user should call 'list_names' method and get the names of the lists the user owns" do
@user.lists.build :name => "list 1"
@user.lists.build :name => "list 2"
@user.lists.build :name => "list 3"
assert_equal ["list 1", "list 2", "list 3"],
@user.list_names
end
test "a user with only one list should only return that list name" do
@user.lists.build :name => "list 1"
assert_equal ["list 1"],
@user.list_names
end
test "a user with no lists should return an empty list" do
assert_equal [],
@user.list_names
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment