Search code examples
ruby-on-railsruby-on-rails-4testingfunctional-testing

how to pass additional data to the test?


please help solve the problem.

controller tags:

class TagsController < ApplicationController
  def show
    @tag = Tag.find(params[:id])
    @posts = @tag.posts
  end
end

test tags:

class TagsControllerTest < ActionController::TestCase
  fixtures :tags, :posts

  setup do
    @tag = tags(:one)        
  end

  test "should get show" do
    get :show, :tag => @tag
    assert_response :success
    assert_not_nil assigns(:tag)
    assert_template :show
    assert_template layout: "layouts/application"        
  end    

  test "should route to show tag posts" do
    assert_generates "/tags/@tag", controller: 'tags', action: 'show', id: "@tag"  # OK
  end  
end

fixtures tags:

one: 
  tagname: 'qwe'

fixtures posts:

one:
  title: 'MyString1'
  body: '1Lorem Ipsum'
  user: :one
  views: 2

after starting the test, I get the following error message:

  1) Error:
TagsControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"tags", :tag=>"980190962"}
    test/controllers/tags_controller_test.rb:20:in `block in <class:TagsControllerTest>'

please tell me how to pass @posts in my test. or tell me another solution

Models Tag and Post linked via HABTM


Solution

  • Your test case should pass :id instead of :tag. It should look like:

      test "should get show" do
        get :show, :id => @tag.id
        assert_response :success
        assert_not_nil assigns(:tag)
        assert_template :show
        assert_template layout: "layouts/application"        
      end