0

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.

1
  • You'd need to capture stdout in order to test the method as it stands. It's doing too much to test easily. Commented Jul 8, 2014 at 23:41

1 Answer 1

2

Add cipher or return cipher after this line

 puts "The encrypted string is: #{cipher}" 

And it should work

To explain the fix given, the last expression in a method is the return value. You've passed the value to STDOUT but not as the return value of the method, so RSpec was failing.

Sign up to request clarification or add additional context in comments.

3 Comments

perfect. I didn't realize interpolation in the last line wouldn't actually count as a return value. By adding in cipher at the end, it worked just like it should. Thanks!
@tomtom To explain the fix given, the last expression in a method is the return value. You've passed the value to STDOUT but not as the return value of the method, so RSpec was failing.
Welcome and I'll add @iain explanation to the answer as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.