Sha256: a2655db746e511953068f150fca7fa721e7c17e9903f3b35980aeae4269d7374

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 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/controller/posts_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controller/posts_controller.rb:3 - use model association"
  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/controller/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/controller/posts_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controller/posts_controller.rb:3 - use model association"
  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/controller/posts_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.1.2 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.1.1 spec/rails_best_practices/checks/use_model_association_check_spec.rb
rails_best_practices-0.1.0 spec/rails_best_practices/checks/use_model_association_check_spec.rb