[rspec-users] Testing url_for
David Chelimsky
dchelimsky at gmail.com
Tue Oct 27 11:17:19 EDT 2009
On Oct 27, 2009, at 8:21 AM, Rodrigo Rosenfeld Rosas wrote:
> Hi David, I'm giving a try to RSpec after we meet each other on
> Rails Summit Latin America and I must admit I'm enjoying using rspec/
> machinist/faker.
>
> Since I have not written any controllers yet, I hadn't taken a
> chance to try webrat.
>
> But there is a situation that I would like some feedback on how to
> deal with it.
>
> When registering new users, they will input their e-mail and a
> message will be sent for them to confirm their addresses and
> continue registering.
>
> I use something like:
>
> MailNotifier.deliver_email_confirmation_message :confirmation_url =>
> url_for(:controllers => 'users', :action =>
> 'continue_register', :user => @user.id, :token =>
> @user.confirmation_token)
>
> And the routes are set to ':controller/:action', so that the url
> would translate to '/users/continue_register?
> user=2&confirmation_token=asdf987asf'.
>
> The problem is that Ruby 1.8 will not maintain any specific order
> for the parameters. (The application is hosted in a shared server at hostingrails.com
> , which hosts Ruby 1.8)
>
> I know that I could add a route to generate '/users/
> continue_register/2/asdf987asf' instead, but I would still like to
> know what would be the alternatives.
Oi Rodrigo,
There's no great alternative that I know of. I've always just grabbed
the URL using a regexp and then broken it up. Something like this:
text.should =~ /http:\/\/test\.host\/users\/continue_register\?([^\s]*)/
query_string = $1
query_string.should =~ /user=2/
query_string.should =~ /confirmation_token=asdf987asf/
You could also use Rack::Utils.parse_query to convert the query_string
to hash, or capture the entire url and use the route_to matcher:
text.should =~ /(http:\/\/test\.host\/users\/continue_register\?[^\s]*)/
{:get => $1}.should route_to(
:controllers => 'users',
:action => 'continue_register',
:user => @user.id,
:token => @user.confirmation_token
)
I don't love either of those - I'd sooner change the implementation to
something deterministic, but that's me :)
>
> How could I verify that the delivered message contains a correct
> url? I know that I should follow the url in an acceptance test, but
> I'm just trying to test that the message is been correct generated,
> in a unit test.
>
> Please, let me know if I missed something conceptually while testing
> this situation.
>
> Thanks for RSpec and the tips about machinist, faker and webrat.
>
> Just one more doubt. When using machinist, is it possible to ignore
> the blueprint while calling 'make' on an ActiveRecord class? I had
> to create a named blueprint reseting all fields set by the blueprint.
I don't know of a way of to do this, but why do you need to? You may
want to look at a couple of other libraries like Fixjour, Fixture
Replacement and Factory Girl - they serve the same function as
Machinist, but don't (afaik) add methods to ActiveRecord::Base.
> It was nice to meet you.
O prazer foi meu.
Tchau,
David
>
> Best Regards,
>
> Rodrigo.
More information about the rspec-users
mailing list