[rspec-users] Eliminating duplication from tests

Brandon Olivares programmer2188 at gmail.com
Fri Apr 10 09:15:11 EDT 2009


David,

I wanted to ask about another macro I have.

I have something currently called assert_form_fields, that is called like:

Assert_form_fields :field1, :field2, ...

What it does is sets an error for each field, and specifies that the field
should be wrapped in a div with class 'error', and that the other fields are
not wrapped by such a div.

It does it by calling other methods. For each field I have
assert_[field]_has_errors and assert_[field]_does_not_have_errors.

So now that I'm trying to rename these, I'm just not sure what to name this.
What should I keep in mind when trying to write appropriate names for such
things?

Thanks,
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