Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Tidy up more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 29, 2013
1 parent b5a3bbc commit 65d29e6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 49 deletions.
25 changes: 11 additions & 14 deletions spec/model/as_json_spec.rb
@@ -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
15 changes: 5 additions & 10 deletions spec/model/attribute_spec.rb
@@ -1,26 +1,21 @@
require "spec_helper"

class ModelAttributeSpec < Vienna::Model
attribute :first_name
end

describe Vienna::Model do
describe ".attribute" do

let(:model) { ModelAttributeSpec.new }
let(:model) { User.new }

it "should create a reader method for attribute" do
model.should respond_to(:first_name)
expect(model).to respond_to(:foo)
end

it "should create a writer method for attribute" do
model.should respond_to(:first_name=)
expect(model).to respond_to(:foo=)
end

describe "writer" do
it "sets value on model" do
model.first_name = "Tommy"
model.first_name.should eq("Tommy")
model.foo = 'Tommy'
expect(model.foo).to eq('Tommy')
end
end
end
Expand Down
34 changes: 15 additions & 19 deletions spec/model/initialize_spec.rb
@@ -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
5 changes: 2 additions & 3 deletions spec/model/load_spec.rb
Expand Up @@ -2,17 +2,16 @@

describe Vienna::Model do
describe "#load" do

let(:model) { SimpleModel.new }

it "marks the model instance as loaded" do
model.load({})
model.should be_loaded
expect(model).to be_loaded
end

it "marks the model as not being a new record" do
model.load({})
model.should_not be_new_record
expect(model).to_not be_new_record
end
end
end
3 changes: 0 additions & 3 deletions spec/model/persistence_spec.rb
@@ -1,9 +1,6 @@
require 'spec_helper'

describe Vienna::Model do

before { SimpleModel.reset! }

let(:model) { SimpleModel.new }

describe "#did_destroy" do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,6 +8,10 @@ class SimpleModel < Vienna::Model
attribute :name
end

class User < Vienna::Model
attributes :foo, :bar, :baz
end

class AdvancedModel < Vienna::Model
primary_key :title
end
Expand Down

0 comments on commit 65d29e6

Please sign in to comment.