Created
July 14, 2011 15:12
-
-
Save blowmage/1082636 to your computer and use it in GitHub Desktop.
Testing Primer (URUG presentation 7/12/2011)
This file contains 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 List < ActiveRecord::Base | |
belongs_to :user | |
validates_presence_of :name | |
validates_presence_of :user | |
end |
This file contains 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 '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 |
This file contains 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 Task < ActiveRecord::Base | |
belongs_to :list | |
end |
This file contains 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 'test_helper' | |
class TaskTest < ActiveSupport::TestCase | |
# Replace this with your real tests. | |
test "the truth" do | |
assert true | |
end | |
end |
This file contains 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 User < ActiveRecord::Base | |
has_many :lists | |
def list_names | |
lists.map &:name | |
end | |
end |
This file contains 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 '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