Given all the hype over TDD, I decided it was time to dig in and add that to the list of things to study. I'm running into an issue, and I'm 100% certain it's just a function of something being wrong with my tests in RSpec. I'm still brand new to RSpec, so I'm having trouble figuring it out... my method works just fine, but the test for the method does not.
Method Code (I know I can refactor this A LOT. This is one of the first Ruby programs I wrote awhile back, which explains the ugliness)
def caesar_cipher(string,offset) string=string.to_s offset=offset.to_i cipher=[] string.each_byte do |i| #capital letters if (i>64 && i<91) if (i+offset)>90 cipher << (i+offset-26).chr else cipher << (i+offset).chr end elsif (i>96 && i<123) if (i+offset)>122 cipher << (i+offset-26).chr else cipher << (i+offset).chr end else cipher << i.chr end end cipher=cipher.join('') puts "The encrypted string is: #{cipher}" end puts "Enter the string you'd like to encrypt" string=gets.chomp puts "Enter the offset you'd like to use" offset=gets.chomp caesar_cipher(string,offset) Test Code (Just for one generic case, all lower case input)
require './caesarCipher.rb' describe "caesar_cipher" do it 'should handle all lower case input' do caesar_cipher("abcdefg", 3).should == "defghij" end end Method output:
$ ruby caesarCipher.rb Enter the string you'd like to encrypt abcdefg Enter the offset you'd like to use 3 The encrypted string is: defghij Test Output:
$ rspec spec/caesar_cipher_spec.rb Enter the string you'd like to encrypt Enter the offset you'd like to use The encrypted string is: require './caesarCipher.rb' The encrypted string is: defghij F Failures: 1) caesar_cipher should handle all lower case input Failure/Error: caesar_cipher("abcdefg", 3).should == "defghij" expected: "defghij" got: nil (using ==) # ./spec/caesar_cipher_spec.rb:5:in `block (2 levels) in <top (required)>' Finished in 0.00542 seconds (files took 0.14863 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/caesar_cipher_spec.rb:4 # caesar_cipher should handle all lower case input Any help on why the tests are failing? Judging by the output it looks like it's running it twice or something in the tests.