require File.expand_path('../../spec_helper', __FILE__) describe Yammer::Model::Base do class DummyModel < Yammer::Model::Base attr_accessor_deffered :first_name, :last_name end class Yammer::Client def update_dummy_model(id, opts={}) put("/api/v1/dummy_models/#{id}", opts) end end context 'new user object with id' do subject { DummyModel.new(:id => 42) } describe "#id" do it 'should be 42' do expect(subject.id).to eq 42 end end describe "#id=" do it 'sets id' do # subject.send(:id, 2) # expect(subject.id).to eq 2 end end describe "#new_record?" do it 'returns false' do expect(subject.new_record?).to eq false end end describe "#modified?" do context 'new model and no changes' do it 'returns false' do model = DummyModel.new(:first_name => 'jim', :last_name => 'peters') expect(model.modified?).to eq false end end context 'new model and no changes' do it 'returns false' do model = DummyModel.new(:first_name => 'mary', :last_name => 'jane') model.last_name = 'jim' model.last_name = 'smithy' expect(model.modified?).to eq false end end context 'persisted model and no changes' do it 'returns true' do model = DummyModel.new(:id => 12, :first_name => 'jim', :last_name => 'peters') expect(model.modified?).to eq false end end context 'persisted model and changes' do it 'returns true' do model = DummyModel.new(:id => 42, :first_name => 'jim', :last_name => 'peters') model.last_name = 'smithy' expect(model.modified?).to eq true end end end describe "#changes" do it 'returns empty hash' do expect(subject.changes).to eq({}) end end describe "#save" do it 'returns true' do stub_request(:put, "https://www.yammer.com/api/v1/dummy_models/42").with( :body => {"last_name"=>"smithy"}, :headers => { 'Accept' =>'application/json', 'Authorization'=>'Bearer TolNOFka9Uls2DxahNi78A', 'Content-Type' =>'application/x-www-form-urlencoded', 'User-Agent' =>"Yammer Ruby Gem #{Yammer::Version}"}) model = DummyModel.new(:id => 42, :first_name => 'jim', :last_name => 'peters') model.last_name = 'smithy' model.save end end describe "#update" do it 'should update the attributes' do # implement end end describe "#base_name" do it 'returns true' do expect(subject.base_name).to eq 'dummy_model' end end describe "setters" do it 'should update modified attributes hash' do subject end end describe "#save" do context 'unmodified model' do it 'does nothing' do expect(subject.save).to eq subject end end context 'modified model' do subject { DummyModel.new(:id => 42, :first_name => 'jim', :last_name => 'peters') } it 'should update model' do Yammer.should_receive(:update_dummy_model).with(42, hash_including( :first_name =>'john', :last_name => 'smith') ).and_return(double('Response', :success? => true, :created? => true, :body => {:id => 2})) subject.first_name = 'john' subject.last_name = 'smith' subject.save end end context 'unmodified new model' do subject { DummyModel.new(:first_name => 'jim', :last_name => 'peters') } it 'should do nothing' do Yammer.should_receive(:create_dummy_model).with( hash_including(:first_name =>'jim', :last_name => 'peters') ).and_return(double('Response', :success? => true, :created? => true, :body => {:id => '2'})) subject.save end end context 'modified new model' do subject { DummyModel.new(:first_name => 'jim', :last_name => 'peters') } it 'should create model' do Yammer.should_receive(:create_dummy_model).with( hash_including(:first_name =>'john', :last_name => 'peters') ).and_return(double('Response', :success? => true, :created? => true, :body => {:id => 2})) subject.first_name = 'john' subject.save end end end end end