RSpec-Rails bug with to_xml?


#1

Hello, everyone. I’ve been lurking here for a while, but this is my
first post.

I think I’ve run into a RSpec bug in a Rails project I’m working on. I
was working on a few REST controllers, and started getting failures on
a specific spec that verified whether a certain action returned XML
output. I spent quite a lot of time checking my code to see if it was
something I did wrong, but it works when I test it manually.

So I created an empty Rails app, and wrote the bare minimum of code
necessary to reproduce this problem. I’m using Rails 2.1.1, with
rspec-1.1.8 and rspec-rails-1.1.8, all installed as gems.

I started by creating a dead-simple model with two string attributes
and no validations, along with this fixture:

spec/fixtures/users.yml

one:
name: Name
email: email

Then I created a simple controller, and its corresponding spec.

class UsersController < ApplicationController

def index
respond_to do |format|
format.xml { render :xml => User.find(:all).to_xml}
end
end

end

spec

require File.expand_path(File.dirname(FILE) + ‘/…/spec_helper’)

describe UsersController do

fixtures :users

it “should return a XML user list” do
get :index, :format => :xml
response.body.should == User.find(:all).to_xml
end

end

It all looks straightforward enough - I use the same call on both the
controller and the spec, so the two results should indeed be the same.
However, when I run the spec I get this failure:

‘UsersController should return a XML user list’ FAILED
expected: “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<users
type=“array”>\n \n <created-at
type=“datetime”>2008-10-20T11:24:28Z\n
email\n <id type=“integer”>953125641\n
Name\n <updated-at
type=“datetime”>2008-10-20T11:24:28Z\n
\n\n”,
got: " " (using ==)
./spec/controllers/users_controller_spec.rb:10:
/usr/lib64/ruby/1.8/timeout.rb:53:in `timeout’

Finished in 0.256981 seconds

This happens both when using “rake spec” and when running only that
spec file. Firing up the app and accessing localhost:3000/users.xml
returns the correct result. In the “real” project, it’s even weirder:
the “expected” site of the assertion shows a string composed of the
XML out put concatenated to itself, and the “got” side has the correct
output. Something like “Expected ‘aa’ but got ‘a’”.

What could the problem be? Is it really a RSpec bug, or is it
something I did wrong?


Bira



#2

On Mon, Oct 20, 2008 at 7:30 AM, Bira removed_email_address@domain.invalid wrote:

rspec-1.1.8 and rspec-rails-1.1.8, all installed as gems.

spec

end
expected: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<users
Finished in 0.256981 seconds
something I did wrong?
Look up “integrate_views” on the rspec-rails docs:
http://rspec.rubyforge.org/rspec-rails/1.1.8/

Also there is a section called “Integration Model”. Read that. If you
have any further questions don’t hesitate to ask. HTH,


Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com


#3

On 2008-10-20, at 09:19, Zach D. wrote:

Look up “integrate_views” on the rspec-rails docs:
http://rspec.rubyforge.org/rspec-rails/1.1.8/

Also there is a section called “Integration Model”. Read that. If you
have any further questions don’t hesitate to ask. HTH,

Hi Zach. I searched for “integration” on that page, searched Google,
and did a Google site-search on rspec.info and rspec.rubyforge.org for
“integration model”, but no relevant hits came up. When you have a
minute, would you mind sending a link our way, please?

Thanks,
Nick


#4

On Mon, Oct 20, 2008 at 11:24 AM, David C. removed_email_address@domain.invalid
wrote:

a Google site-search on rspec.info and rspec.rubyforge.org for “integration
model”, but no relevant hits came up. When you have a minute, would you mind
sending a link our way, please?

It’s Integration Mode, not Model :slight_smile:

Sorry about that Nick, typo on my end!

http://rspec.rubyforge.org/rspec-rails/1.1.8/classes/Spec/Rails/Example/ControllerExampleGroup.html

Cheers,
David


Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com


#5

On Mon, Oct 20, 2008 at 10:21 AM, Nick H. removed_email_address@domain.invalid
wrote:

model", but no relevant hits came up. When you have a minute, would you mind
sending a link our way, please?

It’s Integration Mode, not Model :slight_smile:

http://rspec.rubyforge.org/rspec-rails/1.1.8/classes/Spec/Rails/Example/ControllerExampleGroup.html

Cheers,
David


#6

On 2008-10-20, at 11:24, David C. wrote:

It’s Integration Mode, not Model :slight_smile:

http://rspec.rubyforge.org/rspec-rails/1.1.8/classes/Spec/Rails/Example/ControllerExampleGroup.html

Ah! Thanks for the clarification, David.

No worries about the typo, Zach. My fingers often have a mind of their
own, too.
-Nick