Sha256: f80ac686d15957aeaa272773a4e0b4fc3dd5e0d0b66847a5bddd8631c4a2f95a

Contents?: true

Size: 1.84 KB

Versions: 31

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

describe RailsBestPractices::Reviews::UseModelAssociationReview do
  let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::UseModelAssociationReview.new) }

  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.review('app/controllers/posts_controller.rb', content)
    runner.should have(1).errors
    runner.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.review('app/controllers/posts_controller.rb', content)
    runner.should have(0).errors
  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.review('app/controllers/posts_controller.rb', content)
    runner.should have(1).errors
    runner.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.review('app/controllers/posts_controller.rb', content)
    runner.should have(0).errors
  end
end

Version data entries

31 entries across 31 versions & 3 rubygems

Version Path
rails_best_practices-gorgeouscode-1.0.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.9.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.8.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.7.2 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.7.1 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.7.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.6.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.5.3 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.5.2 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.5.1 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.5.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.4.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.3.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.2.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.1.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.0.1 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-1.0.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-0.10.1 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-0.10.0 spec/rails_best_practices/reviews/use_model_association_review_spec.rb
rails_best_practices-raydog153-0.9.1 spec/rails_best_practices/reviews/use_model_association_review_spec.rb