This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
b5a3bbc
commit 65d29e6
Showing
6 changed files
with
37 additions
and
49 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
class AsJsonSpec < Vienna::Model | ||
attributes :first_name, :last_name | ||
end | ||
|
||
describe Vienna::Model do | ||
|
||
let(:model) { AsJsonSpec.new } | ||
|
||
describe "#as_json" do | ||
let(:model) { User.new } | ||
|
||
it "returns a hash" do | ||
model.as_json.should be_kind_of(Hash) | ||
expect(model.as_json).to be_kind_of(Hash) | ||
end | ||
|
||
it "contains all attributes on model" do | ||
model.as_json.should == { "first_name" => nil, "last_name" => nil } | ||
expect(model.as_json).to eq({ "foo" => nil, "bar" => nil, "baz" => nil }) | ||
|
||
model.foo = "Adam" | ||
expect(model.as_json).to eq({ "foo" => "Adam", "bar" => nil, "baz" => nil }) | ||
|
||
model.first_name = "Adam" | ||
model.as_json.should eq({ "first_name" => "Adam", "last_name" => nil }) | ||
model.bar = "Beynon" | ||
|
||
model.last_name = "Beynon" | ||
model.as_json.should eq({ "first_name" => "Adam", "last_name" => "Beynon" }) | ||
expect(model.as_json).to eq({ "foo" => "Adam", "bar" => "Beynon", "baz" => nil }) | ||
end | ||
|
||
it "includes the id, if set" do | ||
model.id = 42 | ||
model.as_json.should eq({ "id" => 42, "first_name" => nil, "last_name" => nil }) | ||
|
||
expect(model.as_json).to eq({ "id" => 42, "foo" => nil, "bar" => nil, "baz" => nil }) | ||
end | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,42 @@ | ||
require "spec_helper" | ||
|
||
class ModelInitializeSpec < Vienna::Model | ||
attributes :foo, :bar, :baz | ||
end | ||
|
||
describe Vienna::Model do | ||
describe "#initialize" do | ||
it "should have all nil values for attributes when passed no attrs" do | ||
model = ModelInitializeSpec.new | ||
model = User.new | ||
|
||
model.foo.should be_nil | ||
model.bar.should be_nil | ||
model.baz.should be_nil | ||
expect(model.foo).to be_nil | ||
expect(model.bar).to be_nil | ||
expect(model.baz).to be_nil | ||
end | ||
|
||
it "should set a given value for a given attributes" do | ||
model = ModelInitializeSpec.new foo: 3.142 | ||
model = User.new foo: 3.142 | ||
|
||
model.foo.should eq(3.142) | ||
model.bar.should be_nil | ||
model.baz.should be_nil | ||
expect(model.foo).to eq(3.142) | ||
expect(model.bar).to be_nil | ||
expect(model.baz).to be_nil | ||
end | ||
|
||
it "should be able to set many attributes" do | ||
model = ModelInitializeSpec.new foo: 'hello', bar: 'world', baz: 42 | ||
model = User.new foo: 'hello', bar: 'world', baz: 42 | ||
|
||
model.foo.should eq('hello') | ||
model.bar.should eq('world') | ||
model.baz.should eq(42) | ||
expect(model.foo).to eq('hello') | ||
expect(model.bar).to eq('world') | ||
expect(model.baz).to eq(42) | ||
end | ||
|
||
it "creates @attributes as an empty hash" do | ||
model = ModelInitializeSpec.new | ||
model = User.new | ||
model.instance_variable_get(:@attributes).should eq({}) | ||
end | ||
|
||
it "marks the model as being a new record" do | ||
ModelInitializeSpec.new.should be_new_record | ||
expect(User.new).to be_new_record | ||
end | ||
|
||
it "marks the model as not being loaded" do | ||
ModelInitializeSpec.new.should_not be_loaded | ||
expect(User.new).to_not be_loaded | ||
end | ||
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
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
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