<commit_before>require 'test_helper'

class Post
  extend ActiveModel::Naming
end

class Comment
  extend ActiveModel::Naming
end

class CommentsController < InheritedResources::Base
  belongs_to :post
end

class BelongsToTest < ActionController::TestCase
  tests CommentsController

  # TODO: look into the failures as this should run randomly
  def self.test_order
    # version 5 defaults to random, which fails...
    MiniTest::Unit::VERSION.to_i >= 5 ? :alpha : super
  end

  def setup
    draw_routes do
      resources :comments, :posts
    end

    Post.expects(:find).with('37').returns(mock_post)
    mock_post.expects(:comments).returns(Comment)

    @controller.stubs(:resource_url).returns('/')
    @controller.stubs(:collection_url).returns('/')
  end

  def teardown
    clear_routes
  end

  def test_expose_all_comments_as_instance_variable_on_index
    Comment.expects(:scoped).returns([mock_comment])
    get :index, params: { post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal [mock_comment], assigns(:comments)
  end

  def test_expose_the_requested_comment_on_show
    Comment.expects(:find).with('42').returns(mock_comment)
    get :show, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_expose_a_new_comment_on_new
    Comment.expects(:build).returns(mock_comment)
    get :new, params: { post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_expose_the_requested_comment_on_edit
    Comment.expects(:find).with('42').returns(mock_comment)
    get :edit, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_create_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    @controller.expects(:parent_url).returns('http://test.host/')
    Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(save: true))
    post :create, params: { post_id: '37', comment: {these: 'params'} }
    assert_redirected_to 'http://test.host/'
  end

  def test_expose_a_newly_create_comment_on_create
    Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(save: true))
    post :create, params: { post_id: '37', comment: {these: 'params'} }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_update_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    Comment.stubs(:find).returns(mock_comment(update_attributes: true))
    @controller.expects(:parent_url).returns('http://test.host/')
    put :update, params: { id: '42', post_id: '37', comment: {these: 'params'} }
    assert_redirected_to 'http://test.host/'
  end

  def test_update_the_requested_object_on_update
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
    put :update, params: { id: '42', post_id: '37', comment: {these: 'params'} }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_destroy_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:destroy)
    @controller.expects(:parent_url).returns('http://test.host/')
    delete :destroy, params: { id: '42', post_id: '37' }
    assert_redirected_to 'http://test.host/'
  end

  def test_the_requested_comment_is_destroyed_on_destroy
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:destroy)
    delete :destroy, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def helper_methods
    @controller.class._helpers.instance_methods.map {|m| m.to_s }
  end

  def test_helpers
    mock_post.stubs(:class).returns(Post)

    Comment.expects(:scoped).returns([mock_comment])
    get :index, params: { post_id: '37' }

    assert helper_methods.include?('parent?')
    assert @controller.send(:parent?)
    assert_equal mock_post, assigns(:post)
    assert helper_methods.include?('parent')
    assert_equal mock_post, @controller.send(:parent)
  end

  protected

    def mock_post(stubs={})
      @mock_post ||= mock(stubs)
    end

    def mock_comment(stubs={})
      @mock_comment ||= mock(stubs)
    end
end
<commit_msg>Remove unncessary stub call that causes failure<commit_after>require 'test_helper'

class Post
  extend ActiveModel::Naming
end

class Comment
  extend ActiveModel::Naming
end

class CommentsController < InheritedResources::Base
  belongs_to :post
end

class BelongsToTest < ActionController::TestCase
  tests CommentsController

  # TODO: look into the failures as this should run randomly
  def self.test_order
    # version 5 defaults to random, which fails...
    MiniTest::Unit::VERSION.to_i >= 5 ? :alpha : super
  end

  def setup
    draw_routes do
      resources :comments, :posts
    end

    Post.expects(:find).with('37').returns(mock_post)
    mock_post.expects(:comments).returns(Comment)

    @controller.stubs(:resource_url).returns('/')
    @controller.stubs(:collection_url).returns('/')
  end

  def teardown
    clear_routes
  end

  def test_expose_all_comments_as_instance_variable_on_index
    Comment.expects(:scoped).returns([mock_comment])
    get :index, params: { post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal [mock_comment], assigns(:comments)
  end

  def test_expose_the_requested_comment_on_show
    Comment.expects(:find).with('42').returns(mock_comment)
    get :show, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_expose_a_new_comment_on_new
    Comment.expects(:build).returns(mock_comment)
    get :new, params: { post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_expose_the_requested_comment_on_edit
    Comment.expects(:find).with('42').returns(mock_comment)
    get :edit, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_create_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    @controller.expects(:parent_url).returns('http://test.host/')
    Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(save: true))
    post :create, params: { post_id: '37', comment: {these: 'params'} }
    assert_redirected_to 'http://test.host/'
  end

  def test_expose_a_newly_create_comment_on_create
    Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(save: true))
    post :create, params: { post_id: '37', comment: {these: 'params'} }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_update_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    Comment.stubs(:find).returns(mock_comment(update_attributes: true))
    @controller.expects(:parent_url).returns('http://test.host/')
    put :update, params: { id: '42', post_id: '37', comment: {these: 'params'} }
    assert_redirected_to 'http://test.host/'
  end

  def test_update_the_requested_object_on_update
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
    put :update, params: { id: '42', post_id: '37', comment: {these: 'params'} }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def test_redirect_to_the_post_on_destroy_if_show_and_index_undefined
    @controller.class.send(:actions, :all, except: [:show, :index])
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:destroy)
    @controller.expects(:parent_url).returns('http://test.host/')
    delete :destroy, params: { id: '42', post_id: '37' }
    assert_redirected_to 'http://test.host/'
  end

  def test_the_requested_comment_is_destroyed_on_destroy
    Comment.expects(:find).with('42').returns(mock_comment)
    mock_comment.expects(:destroy)
    delete :destroy, params: { id: '42', post_id: '37' }
    assert_equal mock_post, assigns(:post)
    assert_equal mock_comment, assigns(:comment)
  end

  def helper_methods
    @controller.class._helpers.instance_methods.map {|m| m.to_s }
  end

  def test_helpers
    Comment.expects(:scoped).returns([mock_comment])
    get :index, params: { post_id: '37' }

    assert helper_methods.include?('parent?')
    assert @controller.send(:parent?)
    assert_equal mock_post, assigns(:post)
    assert helper_methods.include?('parent')
    assert_equal mock_post, @controller.send(:parent)
  end

  protected

    def mock_post(stubs={})
      @mock_post ||= mock(stubs)
    end

    def mock_comment(stubs={})
      @mock_comment ||= mock(stubs)
    end
end

{'fraction_non_alphanumeric': 0.1308699818975615, 'fraction_numerical': 0.010222553508678522, 'mean_word_length': 4.779692307692308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6852516', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3080, 'n_words': 591}