[rspec-users] Eliminating duplication from tests
Brandon Olivares
programmer2188 at gmail.com
Fri Apr 10 08:16:56 EDT 2009
David,
Thank you very much. Most of my methods that are generating examples are
similar to that, and I hadn't thought to call them macros.
Anyway I like the name you suggest. I'll go through my macros and try to
come up with more appropriate names for them.
Oh, and that described_class trick is great; thanks for showing me that.
Brandon
> -----Original Message-----
> From: rspec-users-bounces at rubyforge.org [mailto:rspec-users-
> bounces at rubyforge.org] On Behalf Of David Chelimsky
> Sent: Friday, April 10, 2009 3:57 AM
> To: rspec-users
> Subject: Re: [rspec-users] Eliminating duplication from tests
>
> On Fri, Apr 10, 2009 at 4:38 AM, Brandon Olivares
> <programmer2188 at gmail.com> wrote:
> >
> >
> >> -----Original Message-----
> >> From: rspec-users-bounces at rubyforge.org [mailto:rspec-users-
> >> bounces at rubyforge.org] On Behalf Of David Chelimsky
> >> Sent: Friday, April 10, 2009 3:10 AM
> >> To: rspec-users
> >> Subject: Re: [rspec-users] Eliminating duplication from tests
> >>
> >> Can you please post specific examples of the use of these so we
> don't
> >> have to talk in generalities?
> >
> > Sure, here is something I just wrote.
> >
> > def assert_route_recognition path, controller, methods
> > describe "route recognition" do
> > [:get, :post, :put, :delete].each do |method|
> > if methods.include? method
> > it "should generate the params" +
> > " {:controller => '#{controller}', :action =>
> > '#{methods[method]}'}" +
> > " when the request method is #{method.to_s.upcase}" do
> > params_from(method, path).should == {
> > :controller => controller,
> > :action => methods[method]
> > }
> > end
> > else
> > it "should not accept the #{method} method" do
> > lambda {
> > params_from(method, path)
> > }.should raise_error(ActionController::MethodNotAllowed)
> > end
> > end
> > end
> > end
> > end # assert_route_recognition
>
> I meant where it's used, not how it works :) But now that you've shown
> the implementation, I can see that this is a macro that generates
> examples. So when you're using it it probably looks like this:
>
> describe WidgetsController do
> assert_route_recognition "/widgets/", :widgets, [:get, :post]
> end
>
> In this case, I'd change the name to something like
> recognizes_routes_for:
>
> describe WidgetsController do
> recognizes_routes_for "/widgets/", :widgets, [:get, :post]
> end
>
> But even that is a bit of a challenge to understand what all the
> arguments mean. Another option might be:
>
> describe WidgetsController do
> recognizes_routes_for :get, :post, "/widgets/"
> end
>
> The controller can be inferred from WidgetsController in the macro,
> which you can access from the described_class method:
>
> :controller =>
> described_class.to_s.sub(/Controller/,'').underscore.to_sym
>
> >> Of course your "testing" is progressing towards something more
> >> "test-like." You're calling it "testing" so you're probably thinking
> >> of it as "testing." You're using words like "ensure" instead of
> >> "specify" and you've even named your expectations "assert_xxx"
> instead
> >> of "expect_xxx."
> >
> > Interesting. I'm just used to thinking that way I guess. What is
> wrong with
> > ensure instead of specify?
> >
> > And I've never seen that convention with the expect prefix. Can you
> provide
> > an example?
>
> I don't have any examples :) I was just making a suggestion because
> part of the BDD nomenclature is "expectation" instead of "assertion."
> An expectation is about something in the future. An assertion is about
> something that already exists.
>
> In this case, since it's a macro, I'd go with what I suggested above
> (recognizes_routes_for).
>
> HTH,
> David
>
> > Thank you very much for the help.
> >
> > Brandon
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
More information about the rspec-users
mailing list