Writing first Rspec

44 views
Skip to first unread message

Aravindhan A

unread,
Jun 25, 2015, 3:27:14 AM6/25/15
to rs...@googlegroups.com
Hello,

I have come up with the following code.  It involves checking a sentence with some conditions.  I am in need of writing a RSpec for this particular file.

Class Sentence
def test(sen)

first = sen[0]
last = sen.split('').last
rest = sen[1..-1]
word_arr = sen.split
word_arr_len = word_arr.length

unless first == first.upcase then
puts "The first letter is not in uppercase"
end

if  word_arr_len == 1 then
puts "There must be spaces between words"
end

unless last == "." || last = last.upcase then
puts "The sentence should end with a full stop"
end

unless (sen =~ /[A-Z]{2,}/) == nil then
puts "No two consecutive upper case letters are allowed"
end

unless sen == word_arr.join(" ")  then
puts "No two consecutive spaces are allowed"
end
end
end


#test("Simple sentence.")


I want to write a simple RSpec code that passes a value to the test method [Similar to the last commented line].  Kindly help me out!

Thanks in advance.

Myron Marston

unread,
Jun 25, 2015, 4:08:00 AM6/25/15
to rs...@googlegroups.com, aravin...@gmail.com
The `output` matcher will work well here.  I've put together the start of what the specs could be:


This includes one spec for the "happy path" case (where it passes all checks and prints nothing) and one spec for one of the validations.  You should be able to follow the example to come up with specs for the other validations.

HTH,
Myron

Aravindhan A

unread,
Jun 25, 2015, 4:45:51 AM6/25/15
to rs...@googlegroups.com, aravin...@gmail.com
Hello Myron,

Thank you for solving the problem.

I have few more queries:

When I copy the code and execute in my remote system, it shows the error uninitialized constant RSpec.

I would like to know how to separate out the RSpec file from the class file and how to run them in command line & what are the naming conventions of RSpec file.  I didn't wrote any RSpec file so far, so your help is required.

Thanks in advance!

Jon Rowe

unread,
Jun 25, 2015, 6:27:27 AM6/25/15
to rs...@googlegroups.com
Hi

The convention is put your spec files in the `/spec` directory, with file names matching your application code appended by `_spec.rb`. 

For example if you have `lib/sentence.rb`, then you’d have `spec/sentence_spec.rb`.

You also generally run RSpec files with the `rspec` command, which by default will run all `_spec.rb` files in the `/spec` directory. 

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/cee8cd5a-1a6c-4f46-aae0-7693f5fa76bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages