Named routes problem more rails than rspec

Howdy,

I have emailed before about the inconsistencies I’m spotting when using
named routes/resource, but I think I’ve narrowed down the issue. It
would
seem this has nothing to do with rspec per se, but more with how routing
works with rails.

If you have an object instance, that isn’t saved and you attempt to
generate
a path for that item, even though the item may have an ID, you get a “No
route matches” exception.

If the item is saved, then a valid route can be generated for that item.

Two things seem odd:

  1. The exception raised isn’t very informative
  2. Why is this a constraint? And can it be turned off for testing
    purposes?

Is there a way around this? Or do I need to use rspec mock objects?

Cheers,
Jason

On Jan 30, 2011, at 3:48 AM, Jason N. wrote:

Why is this a constraint? And can it be turned off for testing purposes?
If that’s how the framework works and it’s an internal thing like this,
turning it off for testing purposes would just lead to confusion.
Is there a way around this? Or do I need to use rspec mock objects?

You can either use mock_model or mock_stub (which each act like saved
objects unless you tell them otherwise), or use real objects saved to
the db.

HTH,
David

On Sun, Jan 30, 2011 at 9:09 AM, David C. [email protected]
wrote:

You can either use mock_model or mock_stub

David,

Did you mean to say stub_model rather than mock_stub?


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Jan 30, 2011, at 9:34 AM, Rick DeNatale wrote:

On Sun, Jan 30, 2011 at 9:09 AM, David C. [email protected] wrote:

You can either use mock_model or mock_stub

David,

Did you mean to say stub_model rather than mock_stub?

Yes :slight_smile:

Thanks for that… although the exception still bugs me… and digging
through the router code in rails is mighty obtuse.

I’m switching over to rspec mocks to get me over this… (was using
mocha).

One problem I ran into, I’m testing a mailer (+ view), and I’m trying to
generate the email ONCE for the entire spec, and then run various it {
should have_body_text(x) }

If I generate the email once using before(:all), I don’t have access to
any
mock methods (which is understandable). Is there a way to get this
going? Or
am I going to have to do the whole thing in one big “it” block?

Cheers,
Jason

On Jan 30, 2011, at 5:53 PM, Jason N. wrote:

Yes :slight_smile:

Thanks for that… although the exception still bugs me… and digging through
the router code in rails is mighty obtuse.

I’m switching over to rspec mocks to get me over this… (was using mocha).

One problem I ran into, I’m testing a mailer (+ view), and I’m trying to
generate the email ONCE for the entire spec, and then run various it { should
have_body_text(x) }

If I generate the email once using before(:all), I don’t have access to any mock
methods (which is understandable). Is there a way to get this going?

Yes - you’ve got it in front of you, but you don’t seem to want to do it
:slight_smile:

Use mocks in before(:each).

Or am I going to have to do the whole thing in one big “it” block?

That’s an alternative too, but I’d recommend what I wrote above.

FWIW,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs