Hi Ben,

On 1 Jun 2012, at 16:46, Ben Densmore wrote:

> I recently wrote my first gem and am now in the process of learning how
> to write tests for it before I release.
> 
> My gem is pretty basic and really just has a few class files with some
> methods for using GET  to an API that returns some JSON data.
> 
> I'm trying to come up with what I should be testing for, but to be
> honest this is where testing has always stumped me.
> 
> If I have a class file that looks like the following:
> 
> module MyModule
> 
>    class MyClass < MyParentClass
> 
>      def search(keyword)
>         MyModule.get("/someurl/#{keyword}")
>      end
> 
>    end
> 
> end
> 
> The method in this class uses the HTTParty gem and just does a get on
> this URL which returns JSON.
> 
> Is it a valid use case to test for the keyword argument? I am not sure
> if there is any benefit to testing this since it's so very basic. It's a
> good experiment for me since I am so new to testing but I really want to
> understand what specifically I should be testing.
> 
> Any guidance would be greatly appreicated.
> 
> Thanks,
> Ben

You want to aim to test logic, or behaviour. Think about each test as a warning 
light that you're fitting to your system; if someone comes along later and 
inadvertently changes the behaviour, the warning light will go off and alert 
them to their mistake.

In this example, the only behaviour you have is mapping the keyword into a URL, 
so your warning light could check something like 

    describe MyClass
      context "searching" do
        it "calls the web service with the correct URL" do
          MyModule.should_receive(:get).with("/someurl/example-keyword")
          MyClass.new.search('example-keyword')
        end
      end
    end

Because there's so little actual behaviour in the example you've given above, 
this test looks a bit pointless. If you have some more examples of code where 
there's more risk that something could get broken in the future, let us see 
that and we can probably give you more useful advice.

> 
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book
Founder, http://www.relishapp.com/
Twitter, https://twitter.com/mattwynne


_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to