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.4.0 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.27 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.26 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.25 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.24 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.23 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.22 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.21 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.20 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.19 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.18 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.17 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.16 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.15 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.14 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.13 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.12 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.11 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.10 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.3.9 spec/rails_best_practices/checks/use_model_association_check_spec.rb