Posts Tagged ‘Test Driven Development


to RSpec or not to RSpec

I took another look at RSpec today. I tried to re-write the script using a test driven development method but am finding it harder to use than expected. I think I just have to shift my brain into programming TDD style. I really didn’t come up with any good code, I must have started over at least 10 times. I started with things like it “should accept a parameter –operation that takes either ‘add’ or ‘sub’ as it’s value” Then I would read RSpec docs and try to test tha…needless to say it’s a good way to learn about RSpec albeit rather slow. Hopefully I can move my brain to this new style of thinking. It seemed so easy at first but once you try it, it’s actually kind of difficult to do.

Here is the list of requirements I’m going to be working off of.

  • it “should accept two parameters, an operation that is either add or sub and a time value formatted at least with one digit. It should also accept two arguments, an in_file and na out_file”
  • it “should add or subtract time based on the set operation”
  • it “should modify the time by the amount passed in as a parameter of –time”
  • it “should read times and all other lines from the first file”
  • it “should write modified times and unmodified other lines to the second file”

IF you can get these tests to pass, then you’ve got yourself a working program and RSpec test to go with it as well as some documentation!

Until next time…


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 =
        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!


How often I post

May 2020

This is everything