Sha256: 05c724932ae55065d43fb437c04b8698992c4824b470d8d9d71b18b75b3d39e0

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'spec_helper'

module TransForms
  describe MainModel do
    let(:user) { User.create(name: 'John') }
    let(:phone_number) { PhoneNumber.create(user_id: user.id, number: '+1-555-MY-NUMBER') }

    describe 'set_main_model' do
      it 'defines accessor attributes for main model and stores them in a class attribute' do
        form = MainModelModel.new
        expect{ form.user }.to raise_error
        expect{ form.user = User.new }.to raise_error

        MainModelModel.set_main_model :user
        form = MainModelModel.new
        expect{ form.user }.not_to raise_error
        expect{ form.user = User.new }.not_to raise_error

        expect(MainModelModel.main_model).to eq :user
      end
      it 'proxies +model_name+ method to the main_model when proxy: true is set' do
        expect(UserProxyModel.main_model).to eq :user
        expect(UserProxyModel.name).to eq 'UserProxyModel'
        expect(UserProxyModel.model_name.name).to eq 'User'
      end
    end

    describe 'model accessor' do
      it 'can initialize a form model and automatically set the accessor defined by main_model' do
        expect(UserUpdater.main_model).to eq :user
        form = UserUpdater.new(model: user)
        expect(form.user).to eq user
      end

      it 'raises an error if the model supplied is not the type of main model' do
        expect(UserUpdater.main_model).to eq :user
        expect { UserUpdater.new(model: phone_number) }.to raise_error
      end
    end

    describe 'model_name' do
      it 'does not raise an error when raised' do
        #TODO: Improve this spec
        expect { UserUpdater.model_name }.not_to raise_error
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trans_forms-0.1.0 spec/trans_forms/main_model_spec.rb