Chloe
Chloe

Reputation: 26294

Why is my integration test failing for get? "ArgumentError: bad argument (expected URI object or URI string)"

Why is this test giving this error?

Error

  1) Error:
PostIntegrationTest#test_should_not_show_comment_box_if_not_logged_in:
ArgumentError: bad argument (expected URI object or URI string)
    test/integration/post_integration_test.rb:6:in `block in <class:PostIntegrationTest>'

Code, post_integration_test.rb

require 'test_helper'

class PostIntegrationTest < ActionDispatch::IntegrationTest

  test "should not show comment box if not logged in" do
    get :show, 'id' => 1                       ########### LINE 6
    assert_select 'textarea', false, "Comment textarea must not exist if not logged in"
  end

Also doesn't work

get :show, {'id' => 1}
get :show, {id: 1}

Reference

This says you can pass arguments. http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-get

This is an example of using parameters to get: http://guides.rubyonrails.org/testing.html#setup-and-teardown

Version

$ rails -v
Rails 4.0.0

Upvotes: 3

Views: 1516

Answers (2)

Malik Shahzad
Malik Shahzad

Reputation: 6959

From the examples given in Rails Testing Guide we see that, in Integration Tests get/post/put/delete has a url as first argument. While Controllers Tests has the name of action.

Its because in controller test minitest knows that of which controller's actions(show, destroy, new, create) is being called, because we specified the that Controller Name in our test class name. And so in Integration Tests we must pass the url.

Upvotes: 4

vee
vee

Reputation: 38645

:show is not available in Integration tests, the actions are only available in Controller tests. You need to either use _path helper or string representation of your url.

test "should not show comment box if not logged in" do
  # Assuming path is /posts.  Replace accordingly.
  get "/posts/1"                      ########### LINE 6
  assert_select 'textarea', false, "Comment textarea must not exist if not logged in"
end

Upvotes: 5

Related Questions