Sha256: f95ada3ac6095bf92ae602bd31d10f4524706fab427a21c601ef26d522ab135f

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

describe "has_one" do
  extend WebStub::SpecHelpers
  
  it "should define reader" do
    User.new.should.respond_to :profile
  end
  
  it "should define writer" do
    User.new.should.respond_to :profile=
  end
  
  it "should define reset method" do
    User.new.should.respond_to :reset_profile
  end
  
  it "should by default return nil" do
    User.new.profile.should.be.nil
  end
  
  it "should reset" do
    user = User.new
    user.profile = Profile.new
    user.reset_profile
    user.profile.should.be.nil
  end
  
  describe "writer" do
    it "should create model when assigned with hash" do
      user = User.new
      user.profile = { :id => 1, :name => 'John', :email => 'doe@example.com' }
      user.profile.should.is_a Profile
    end
    
    it "should set attributes when assigned with hash" do
      user = User.new
      user.profile = { :id => 1, :name => 'John', :email => 'doe@example.com' }
      user.profile.name.should == 'John'
      user.profile.email.should == 'doe@example.com'
    end
    
    it "should use identity map when assigned with hash" do
      profile = Profile.instantiate(:id => 10)
      user = User.new
      user.profile = { :id => 10 }
      user.profile.should == profile
    end
    
    it "should act like a regular setter when assigned with object" do
      profile = Profile.new
      user = User.new
      user.profile = profile
      user.profile.should == profile
    end
  end
  
  describe "piggybacking" do
    it "should set association when returned with model" do
      stub_request(:get, "http://example.com/users/1.json").to_return(json: { id: 1, profile: { id: 2, name: 'John' } })
      User.find(1) do |result|
        @result = result
        resume
      end
      
      wait_max 1.0 do
        @result.profile.name.should == 'John'
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
motion-resource-0.1.4 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.1.3 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.1.2 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.1.1 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.1.0 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.0.2 spec/motion-resource/associations/has_one_spec.rb
motion-resource-0.0.1 spec/motion-resource/associations/has_one_spec.rb