[rspec-users] Newbie testing questions
matt at mattwynne.net
Fri Jun 1 16:07:37 UTC 2012
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)
> 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.
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
context "searching" do
it "calls the web service with the correct URL" do
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 at rubyforge.org
Freelance programmer & coach
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users