Created
March 21, 2012 11:29
-
-
Save madx/2146318 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module DataAPI | |
class Application < ActiveRecord::Base | |
# Validations ... | |
attr_accessible :name, :domain, :description | |
has_many :rooms, dependent: :destroy | |
end | |
end |
This file contains hidden or 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_relative '../helper' | |
class ApplicationTest < Test | |
def test_it_has_rooms | |
app = create_app | |
room = create_room application: app | |
assert_kind_of Array, app.rooms | |
assert_equal room, app.rooms.first | |
end | |
def test_it_has_dependent_rooms | |
app = create_app | |
room = create_room application: app | |
app.destroy | |
assert_predicate app, :destroyed? | |
assert_predicate room, :destroyed? | |
end | |
end |
This file contains hidden or 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
$ bundle exec ruby test/models/application_test.rb | |
Run options: --seed 30518 | |
# Running tests: | |
F. | |
Finished tests in 0.176238s, 11.3483 tests/s, 34.0448 assertions/s. | |
1) Failure: | |
test_it_has_dependent_rooms(ApplicationTest) [test/models/application_test.rb:25]: | |
Expected #<DataAPI::Room id: 1, application_id: 1, name: "App Room", created_at: "2012-03-21 14:25:09", updated_at: "2012-03-21 14:25:09"> to be destroyed?. | |
2 tests, 6 assertions, 1 failures, 0 errors, 0 skips |
This file contains hidden or 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
module DataAPI | |
class Room < ActiveRecord::Base | |
attr_accessible :name, :application | |
belongs_to :application | |
end | |
end |
This file contains hidden or 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
def create_app(fields={}) | |
fields.update({ | |
name: "App", | |
domain: "app.cmix.com", | |
description: "An app" | |
}) { |_, old, new| | |
return new unless old | |
old == :null ? nil : old | |
} | |
Application.create(fields) | |
end | |
def create_room(fields={}) | |
fields.update({ | |
name: "App Room", | |
}) { |_, old, new| | |
return new unless old | |
old == :null ? nil : old | |
} | |
Room.create(fields) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment