1
votes

I'd like to put a few integration tests in a separate directory from my controller unit specs. However, when I move my spec file to spec/integration, it fails with:

ArgumentError:
       bad argument(expected URI object or URI string)

The spec passes correctly when in the spec/controllers directory.

Here's a bit from my spec:

require 'spec_helper'

describe Users::LoginsController, type: :controller do
  let!(:user) { User.create(email: '[email protected]', password: 'test')

  it 'logs in the user' do
    post :create, email: '[email protected]', password: 'test'
    controller.current_user.should == user
  end
end

I'm using Rails 3.1.3, RSpec 2.7.0.

Are there any tricks I have to use to achieve this?

6
Forgot to copy that in, fixed the post.Ben Crouse

6 Answers

2
votes

Try specifying type:

describe ProductsController, type: :controller do
  it 'can test #show' do
    get :show
  end
end

Works in Rails 3.2.11

0
votes

You have to do the following:

describe Users::LoginsController do
  controller_name 'users/logins'

  ... the rest of your spec here ...

end

I am not entirely certain about the nested syntax, but at least you need to specify the controller_name to get it to work.

Hope this helps.

0
votes

The test framework does not like it if you specify the test action using a symbol.

it 'logs in the user' do
    post :create, email: '[email protected]', password: 'test'
    controller.current_user.should == user
end

becomes

it 'logs in the user' do
    post 'create', email: '[email protected]', password: 'test'
    controller.current_user.should == user
end
0
votes

I had the same problem and ended up simply using the URL

get "/users"

It's not as clean but gets the job done. I couldn't get the other suggestions to work.

0
votes

This approach works for me in Rails 4 and RSpec 2.14.7:

My findings:

  1. Don't name your directory integration. I need to grep through the source of either RSpec or Rails, but it appears to have an adverse effect on running controller specs. Perhaps someone with this knowledge can chime in to confirm this.
  2. Name it something other than integration; in my case I tried int, which resulted in problems until I added the "type: :controller" after the #describe method.
  3. After that, I was able to move all of my slow Rails specs into my int directory, allowing me to create a unit directory for all of my decoupled, fast specs.

Please let me know if this works for you.

By the way, I am running:

  1. Ruby 1.9.3
  2. Rails 4.0.2
  3. rspec-core 2.14.7
  4. rspec-rails 2.14.1

all on Mac OS X.

-9
votes

Take the opportunity now to get rid of your controller and integration specs. They're needlessly painful to write and too coupled to implementation. Replace them with Cucumber stories. Your life will be much easier...