Shift Subtitle, RSpec

Sometimes it’s just nice to have everything you need in your apartment and you can just sit around and code :]

I decided today to try my hand at some unit testing using RSpec, as was suggested at the end of the challenge. I haven’t done much testing before so this seems like a good way to get started. The code I submitted includes a class definition as well as some imperative programming all in one. It would have been nice to separate that out, but this was supposed to be one script that works using the standard library. However, since this is beyond the challenge I’m going to test the class I’ve made, SubtitleTime. You can see it in the pastebin. It takes two parameters, a string in the format of HH:MM:SS,mmm and a ‘modifier’ that is either add or sub.

So starting off with RSpec first you have to ‘describe’ the class. Then you use a function named ‘it’ and write what the class should do. For example:

describe SubtitleTime do
    it "should be able to add another timestamp to itself" do

Once that part of the code is written, then you just have to add some filler lines to actually test the code. This code above could be expanded to look like this:

describe SubtitleTime do
    it "should be able to add another timestamp to itself" do
        subtitletime.should add_time(another_timestamp)
end # note: this has been crossed out as after reading more about RSpec, this just doesn't make sense.

Well after having written a few test cases and finally understanding what RSpec is all about, I feel like I should do this the right way and see what happens to my class if I built it using TDD (Test Driven Design). It initially seems very difficult to build up a test suite on an already existing piece of code. So let’s get started with some test cases.

First off we want the class to have a time that, before altered, is equal to the initial time. The code for that would look like this:

describe SubtitleTime do
    it "should have time equal to the initial time before the class is altered" do
        subtime = Subtitletime.new(:initial)
        subtitle.time.should == :initial

Then When you run this test with the command <spec subtitletime_spec.rb> it will fail saying there is no class SubtitleTime. Simple. The idea behind TDD is that you make a test case, have it break, then fix it. An occasional (or after each iteration) step of refactoring your code should also be included in this process. So now I’ll make a class SubtitleTime

class SubtitleTime

This fixes the first error but we still get another error as the test is run. But that’s all I have time for right now, I was using an ibm guide to rspec. That should make things fairly clear. There is a lot to go over and a lot to do, but I’ll get back to this hopefully sooner rather than later and then finish up this challenge’s extras!


0 Responses to “Shift Subtitle, RSpec”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


How often I post

September 2009
    Oct »

This is everything


%d bloggers like this: