0

I'm a newbie programmer currently working on the Roman Numeral Kata in Javascript. I've solved it at all specs (I'm using Jasmine) are passing. However, I have a lot of duplication in my specs. Here are just the first two:

describe("RomanNumeral", function () {

  it('returns I for 1', function () {
    expect(roman(1)).toEqual('I')
  })

  it('returns II for 2', function (){
    expect(roman(2)).toEqual('II')
  })

 })

In Ruby I could solve the problem by doing something like this:

describe Roman do 
[
  [1, 'I'],
  [2, 'II']

 ].each do | natural_number, roman_numeral |
    it "converts #{natural_number} to #{roman_numeral}" do 
      Roman.of(natural_number).should == roman_numeral
    end
  end
end

I'm going to use a hash instead of an array but the goal is still the same. I'd like to iterate through that hash, call each, set a natural_number variable, set a roman_numeral variable and then have each spec ran by plugging in the arguments from the hash.

But right now all I've got is

describe("RomanNumeral", function () {

  var test_hash = {1, 'I'}

  var someFunction

  _.each(test_hash, someFunction{ 

  }) 

What is the best way to remove the duplication form my Javascript spec? Thank you!

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
user3245240
  • 265
  • 4
  • 10

1 Answers1

0

You should define your object of roman numerals like so

var numerals = {
    1:'I',
    2:'II'
};

and then you can do

_.each(numerals, function(value, key) {
    it('returns '+value+' for '+key, function () {
        expect(roman(key)).toEqual(value);
    });
});
Danny
  • 7,368
  • 8
  • 46
  • 70