August 11th, 2007 at 12:54 pm
(Behaviour Driven Development, Rails, Ruby)
Chris Wanstrath has written about making Rails fixtures less painful than they need to be with the FixtureScenarios plugin. Personally, I prefer the Factory approach, nicely explained by Daniel Manges.
I've been using factory methods to create in-database ActiveRecord objects for a project that I've been working on in Bezurk. Reading Daniel's article gave me a few ideas on improving the way I create fixtures and mocks. Since I've been using RSpec extensively in this project, I'll present the examples in RSpec.
As the models evolve with the design and its behaviour change accordingly, there is a need to go through all the specifications that create this model and make sure that its created in a valid state. This is more pronounced with the use of test doubles, the test doubles also need to have its method stubs changed to reflect the latest state of the model that its is representing. I happen to make much use of test doubles for test isolation, so trying to manage all these objects became an exercise in patience. As it was getting painful, It's time to change the way I create these models and test doubles.
As always, a layer of indirection will always go some way to solving a software problem. We introduce a Factory that encapsulates the creation of ActiveRecord objects by providing creation methods.
RUBY:
-
module FixtureFactory
-
def create_user(attributes = {})
-
User.create!(ModelAttributes.user(attributes))
-
end
-
end
We'll have a Factory for test doubles too.
RUBY:
-
module MockFactory
-
def mock_user(method_stubs = {})
-
mock_model(User, ModelAttributes.user(method_stubs))
-
end
-
end
And the attributes for this model will be declared in a module that's used by both Factories
RUBY:
-
module ModelAttributes
-
def self.user(attributes)
-
attributes.reverse_merge({:name => 'doug'})
-
end
-
end
The Factory modules are then included in Spec::Runnner
RUBY:
-
Spec::Runner.configure do |config|
-
include FixtureFactory
-
include MockFactory
-
end
The objects can now be created using the factory methods available to all specifications.
RUBY:
-
doug = create_user
-
doppelganger = mock_user
Update
Added links to Chris Wanstrath and Daniel Manges' articles on managing Rails fixtures.
Comments
July 26th, 2007 at 10:52 pm
(Behaviour Driven Development, Ruby)
Here's my slides for the talk on Behaviour Driven Development and RSpec at the July 2007 singapore.rb meeting. It might not make much sense if you weren't at the meeting.
Comments
May 10th, 2007 at 10:36 pm
(Behaviour Driven Development, Ruby)
The release of RSpec 0.9.4 can be considered a landmark release of sorts. With the inclusion of Spec::Ui 0.2.0, RSpec can now perform functional testing of web applications within a browser. Out of the box support is limited to Watir/Internet Explorer for now as the Selenium RC custom matchers are not implemented yet. includes WATIR/Windows and Watirsafari/OSX. The custom matchers for Selenium RC are not implemented yet but its certainly possible to use Selenium with Spec::Ui, it just won't read as nicely compared to WATIR. Spec::Ui also comes with a custom result formatter that packages a screenshot of the browser on spec failure, along with its HTML source in the report.
To use Watirsafari in your functional specs, you'll need to install the gem for it: gem install watirsafari
spec_helper.rb should be updated to include the snippet below (code lifted from the original release announcement):
RUBY:
-
Spec::Runner.configure do |config|
-
config.include Spec::Matchers::Watir
-
end
The specification will need before and after blocks like this:
RUBY:
-
before(:all) do
-
@browser = Watir::Browser.new
-
end
-
-
after(:each) do
-
# This is needed to make screenshots work
-
Spec::Ui::ScreenshotFormatter.browser = @browser
-
end
-
-
after(:all) do
-
@browser.kill! rescue nil
-
end
There are a lot more useful information available in the examples supplied with Spec::Ui. Take a look at the samples to get up and running.
Update
My thanks to Aslak Hellesoy for correcting me on support for WATIR and Selenium.
1 Comment
April 5th, 2007 at 11:34 pm
(Rails, Ruby)
The next singapore.rb meeting will be held on the 19th of April 2007 in the Central Lending Library. The National Library Board of Singapore has kindly provided the use of one of their meeting rooms (complete with WIFI). The (tentative) agenda for the meeting is as follows:
7:00pm Rails Plugins Showcase: Dead simple AJAX and Form testing in Rails - Choon Keat
7:30pm FxRuby and ScreenSvr - Sausheong
8:00pm Q&A, discussions
8:30pm End of meeting
My thanks goes out to the NLB's Ivan Chew for facilitating the use of NLB facilities for a trial session, as well as to Choon Keat and Sausheong for making the presentations.
Comments