RSPEC is the latest cool thing.
Test::Unit is no longer in. It's like having
a Walkman instead of an iPod.
It's difficult to find out how to use RSpec, or what
it is supposed to do. If you run
rake rspec
you get all manner of odd messages.
However, I found a good intro to RSpec here:
blog.davidchelimsky.net/articles/2007/05/14/an-introduction-to-rspec-part-i
Create a new model, Person, with nothing in it.
Create spec/models/person_spec.rb and run it:
cat app/models/person.rb
class Person
end
cat ./spec/models/person_spec.rb
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
describe Person do
it "should be in any roles assigned to it" do
end
end
spec ./spec/models/person_spec.rb --format specdoc
Person
- should be in any roles assigned to it
Finished in 0.012756 seconds
1 example, 0 failures
|
What puzzled me about test-driven-development
is when you write code you know is wrong, because it's the
simplest thing that will make the test pass:
def in_role?(role)
true
end
|
But David Chelimsky explains it perfectly:
'Right now, the only requirement of this system that
we’ve expressed is that a “User should be in any
roles assigned to it”, and the system meets that
requirement. In order to push the code to the
next step, we need to express more requirements
with more executable examples.'
This is what I ended up with:
$ cat app/models/person.rb
class Person
def in_role?(role)
role == @role
end
def assign_role(role)
@role = role
end
$ cat spec/models/person_spec.rb
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
describe Person do
it "should be in any roles assigned to it" do
person = Person.new
person.assign_role( "assigned role" )
person.should be_in_role("assigned role")
end
it "should NOT be in any roles not assigned to it" do
person = Person.new
person.should_not be_in_role("unassigned role")
end
end
$ spec ./spec/models/person_spec.rb --format specdoc
Person
- should be in any roles assigned to it
- should NOT be in any roles not assigned to it
Finished in 0.015811 seconds
2 examples, 0 failures
|
Getting this site up. The people at
railsplayground.com
are fantastic.
After deploying the site, I got this rather cryptic message
from one of their tireless elves:
'I have been asked to install merb- gems of version 1.0.1 when run the site...'
It took a lot of googling, installing and uninstalling gems
and changing dependencies.rb to read
merb_gems_version = "1.0.3"
dm_gems_version = "0.9.7"
When I tried to set db_gems_version to 0.9.7, this happened:
merb -i
Loading init file from /home/kirk/dev/ruby/rubyjunction/config/init.rb
...no such file to load -- do_mysql (LoadError)
|
and when I set it to 0.9.9, this happened:
merb -i
Loading init file from /home/kirk/dev/ruby/rubyjunction/config/init.rb
Loading /home/kirk/dev/ruby/rubyjunction/config/environments/development.rb
~
~ FATAL: The gem dm-core (= 0.9.9, runtime), [] was not found
|
The solution was to set db_gems_version to 0.9.7 in dependencies.rb
and uninstall datamapper and data_objects version 0.9.9:
$ sudo gem uninstall data_objects -v=0.9.9
$ sudo gem uninstall do_mysql -v=0.9.9
$ merb -i
Activating slice 'MerbAuthSlicePassword' ...
irb: warn: can't alias context from irb_context.
irb(main):001:0>
|
(That's good, by the way).
The site is now up and running. But you know that.
Added file uploads - see
www.idle-hacking.com/2007/09/scalable-file-uploads-with-merb/
but the internals of controller uploader.rb
are slightly different, and I created folder public/data
class Uploader < Application
def index
render
end
def upload
FileUtils.mv params[:file][:tempfile].path,
Merb.root+"/public/data/#{params[:file][:filename]}"
redirect "/uploader"
end
end
|
and you need app/views/uploader/index.html.erb:
The next step is to change authentication so
1. I can write on the site
2. Anyone can read it
This was difficult enough in Rails, but more difficult in
Merb. The comments in the Merb code are like this:
# Mixin the salted user mixin
require 'merb-auth-more/mixins/salted_user'
|
means 'if you are logged on' - thanks
singlecell.angryamoeba.co.uk/post/60951656/an-introduction-to-merb-auth-and-the-wonderful-secrets
Now I've done the site in Merb. This shows I am a Ruby developer,
since Merb is difficult. All the documentation is just plain
wrong, etc.. Fortunately, Merb is being taken over by the Ruby on
Rails team, whose motto is "Web development that doesn't hurt".
What I meant to say is the two teams are merging.
So the next plan is to move the site from Merb to Rails. I will
document the process of doing this for the edification of others.
Portland |
|
London |
|