Ben Buckley
2010-May-28 08:23 UTC
[rspec-users] [Code Style] Is there a more ''rspec'' way for me to write this test?
Morning all,
I have been playing with Ruby/RSpec on a small script to try and get to know the
language and its idioms.
I have got my tests working and removed some duplication, but I am conscious the
code looks quite Java-like in its style.
I would appreciate it if some of the seasoned rspecers on this mailing list
might be able to take a look and give any suggestions on how it could be more
ruby-like?
Overview:
TeamCalendar interacts with a Website object to determine if a person had some
activity on a particular day.
The Website object uses Watir to interact with a real website (pulled into a
separate class because I couldn''t work out how to test it).
describe TeamCalendar do
before do
@mock_website = stub("the website").as_null_object
@builder = TeamBuilder.new(@mock_website).member("Ben")
@hronline = TeamCalendar.new(@mock_website,"Ben")
end
it "should return :normal when no calendar entries exist" do
@hronline.dayType("6/27").should == :normal
end
it "should return :public_holiday when website shows a bank holiday"
do
@builder.member("Ben").withCalendarEntry("6/25","holiday.jpg")
@hronline.dayType("6/25").should == :public_holiday
end
it "should return :public_holiday when any other team member shows bank
holiday" do
@builder.member("Ben").withCalendarEntry("1/4","vacation.jpg")
@builder.member("Dave").withCalendarEntry("1/4","holiday.jpg")
@hronline.dayType("1/4").should == :public_holiday
end
end
class TeamBuilder
def initialize(mock_website)
@team = []
@website = mock_website
@website.stub!(:team).and_return(@team)
end
def member(name)
@name = name
@team.push(name) if !@team.include?(name)
self
end
def withCalendarEntry(day, type)
@website.stub!(:imageShownOnDay).with(day, at name).and_return(type)
end
end
Thanks,
-Ben