Rspec doesnt recognize a custom mime type

Hey guys,

I have a custom MIME type registered like that:

cat config/initializers/mime_types.rb

Mime::Type.register “lightbox”, :lightbox

→ This works perfectly well in my application.

However my corresponding specs keep failing, the code:

The controller-spec:

describe UserSessionsController do
describe ‘new action’ do
it ‘should render the new-template on lightbox-request’ do
get :new, :format => :lightbox
response.should render_template(:new)
end
end
end

The controller-action:

def new
@user_session = UserSession.new
respond_to do |format|
format.lightbox do
render :layout => false
end
format.html do
render
end
end
end

Running the spec:

bin/spec spec/controllers/user_sessions_controller_spec.rb -l 25
F
1)
‘UserSessionsController new action should render the new-template on
lightbox-request’ FAILED
expected “new”, got nil
./spec/controllers/user_sessions_controller_spec.rb:
26:
Finished in
0.338956
1 example, 1 failure

With line 26 of the spec being:

response.should render_template(:new)

The interesting thing now is in test.log:

Processing UserSessionsController#new to lightbox (for 0.0.0.0 at
2010-04-29 11:31:41) [GET]
Parameters: {“format”=>:lightbox, “action”=>“new”,
“controller”=>“user_sessions”}
Completed in 84ms (View: 2, DB: 558) | 406 Not Acceptable [http://
test.host/user_sessions/new.lightbox]
SQL (10.0ms) ROLLBACK

Hmmm → 406 Not Acceptable?

However, i can call up http://test.host/user_sessions/new.lightbox
manually in the browser without problems - the view is rendered,
without a layout, just as specified in the controller.

So this seems to be an rspec-specific problem - any ideas on how to
proceed?

Ok,

fixed it, in a nutshell:

Doesnt work:

get :new, :format => :lightbox

Works:

get :new, :format => ‘lightbox’

What was really confusing was that in BOTH cases the same URL appeared
in the log.

Here is my turn on what happened:

Internally - when rails sees a mime-type / format - rails
distinguishes between a string and a symbol, saying that when using
something like

respond_to do |format|
format.lightbox {}
end

“lightbox” is expected to be a string.

When doing a normal request via a browser I passed in the format in
the URL, so as a string.
When running a spec apparently this is passed directly the way I wrote
it, so as a symbol.

That’s why one and the same URL worked in the browser, but not in my
spec.

Does this sound plausible?

On Apr 29, 2010, at 7:40 AM, jollyroger wrote:

get :new, :format => ‘lightbox’
respond_to do |format|
That’s why one and the same URL worked in the browser, but not in my
spec.

Does this sound plausible?

Definitely. I believe what’s happening is that the options hash produced
by the Rails router is a hash with indifferent access (allows you to
access any key as a symbol or a string), but the test helper methods
(get, post, etc) don’t do the same thing.

Glad you solved it.

Cheers,
David

On 2010-04-29 7:40 AM, jollyroger wrote:

get :new, :format => ‘lightbox’
respond_to do |format|
That’s why one and the same URL worked in the browser, but not in my
spec.

Does this sound plausible?

Perfectly. I struggled with this a couple of months ago doing something
like:

get :show, {:id => 1}

and whatever it was in my controller wasn’t expecting an integer, so I
had to change it to:

get :show, {:id => ‘1’}

and it was fine. As I dug into it, I noticed that the params hash in
controllers has everything as strings, and then the light bulb went on
in my head. Since then, I’ve paid more attention to what the params hash
in my specs looks like. And I’m sure you will, too :slight_smile:

Peace,
Phillip