Sha256: 99b7c405614ad88eed2f7c5a92161e8bad5fd484588c58d1c4a1282934fdebab

Contents?: true

Size: 1.98 KB

Versions: 44

Compression:

Stored size: 1.98 KB

Contents

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

describe RailsBestPractices::Checks::UseModelAssociationCheck do
  before(:each) do
    @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseModelAssociationCheck.new)
  end
  
  it "should use model association for instance variable" do
    content = <<-EOF
    class PostsController < ApplicationController
      
      def create
        @post = Post.new(params[:post])
        @post.user_id = current_user.id
        @post.save
      end
    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for @post)"
  end

  it "should not use model association without association assign" do
    content = <<-EOF
    class PostsController < ApplicationController

      def create
        @post = Post.new(params[:post])
        @post.save
      end
    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end

  it "should use model association for local variable" do
    content = <<-EOF
    class PostsController < ApplicationController

      def create
        post = Post.new(params[:post])
        post.user_id = current_user.id
        post.save
      end
    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for post)"
  end

  it "should not use model association" do
    content = <<-EOF
    class PostsController < ApplicationController

      def create
        post = current_user.posts.buid(params[:post])
        post.save
      end
    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
rails_best_practices-0.3.8 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.7 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.6 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.5 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.4 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.3 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.2 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.1 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.0 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.16 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.15 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.14 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.13 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.12 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.11 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.10 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.9 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.8 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.6 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.2.5 spec/rails_best_practices/checks/use_model_association_check_spec.rb