Sha256: ba1c3731e0db36fdb9f19c12f38d6c3a43b1a1185e954c3eda9946e7e7d215be

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require File.join(File.dirname(__FILE__) + '/../../spec_helper')

describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
  before(:each) do
    @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AddModelVirtualAttributeCheck.new)
  end
  
  it "should add model virtual attribute" do
    content = <<-EOF
    class UsersController < ApplicationController
      
      def create
        @user = User.new(params[:user])
        @user.first_name = params[:full_name].split(' ', 2).first
        @user.last_name = params[:full_name].split(' ', 2).last
        @user.save
      end
    end
    EOF
    @runner.check('app/controllers/users_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
  end

  it "should not add model virtual attribute with differen param" do
    content = <<-EOF
    class UsersController < ApplicationController

      def create
        @user = User.new(params[:user])
        @user.first_name = params[:first_name]
        @user.last_name = params[:last_name]
        @user.save
      end
    end
    EOF
    @runner.check('app/controllers/users_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end
  
  it "should not add model virtual attribute with read" do
    content = <<-EOF
    class UsersController < ApplicationController
      
      def show
        if params[:id]
          @user = User.find(params[:id])
        else
          @user = current_user
        end
      end
    end
    EOF
    @runner.check('app/controllers/users_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_best_practices-0.2.3 spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
rails_best_practices-0.2.2 spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
rails_best_practices-0.2.1 spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb