Having read this:
"No route matches" error?
I’m trying to figure out if there is a gem or way to monkey patch actionpack to get around this constraint.
Basically, I’m writing specs (they run fast), and I don’t understand why actionpack throws this error when being applied to an object which isn’t “saved”.
For two reasons:
- Why is it throwing a “No route matches” when it really should be throwing something more meaningful (e.g. object must be saved before a route can be constructed, or object ID is nil). The exception seems a little obscure.
- I shouldn’t have to save the object at all if all I am trying to do is generate a url for that object, given the ID is populated using a factory or something similar.
This constraint makes it a pain to write fast tests, unless I’m missing something…
True, the error message is a bit obscure. Regarding your second point, you don’t need to save an object to generate a URL, the helper will work just as well with a literal value.
So in the example the object can be replaced with any value:
For example, if you use
rails generate scaffold Article, RSpec will build a spec like this:which does not hit the database.