As a perl developer for 11 years ( i started before graduation ) and doing test driven development since Realestate.com.au ( say 5 yrs back ), I am very supportive and believe in TDD. My daily work doesn't really involve a clear MVC framework and doesn't believe in TDD or automated regression tests. Anyway, with rails, I have been able to develop a site with minimal tests (Test::Unit with rails 2.3.x) but I figured it's about time I embraced something that's more industry preferred - RSpec and its friend, cucumber.
rspec - view specs
before()
-any source code found here will run before the tests/specs
-there are 3 values for the single argument this method accepts:
- each - this is default. It does get executed for each spec but gains in terms of reliability as instance variable values often get reinitialised
- all - this gets run exactly once. Its instance variables will be shared across with other specs. The danger here is that if specs change the values of instance variables, this could result in unwanted states of the instance variables
- scope
In reference to the Rspec book, I've been a bit confused with some elements of view specs.
1) let()
2) assign()
Looking at page 85's example:":symbol" is being used when it's being called in methods like let but when it's to be used, we loose the ":".
describe Game do
let( :output ) { .... } <---- the contents of the block are assigned to the symbol, :output
let(:game) {......}
describe "start game" do
it "sends a welcoming message" do
output.should_receive(:puts) .... <---- observe how output is used without the ":"
game.start('player name')...
-----------
it's a hard one to stomach and utterly stupid but I think it's one of those things (the rails way).
Similar to our example,
describe "parts/new.html.erb" do
let(:part) { <----- the mocked model mocking operation is assigned to a method with the symbol of :part. I think it's because the mocked model is to be assigned to the @part instance variable in the view (also refered to as :part in the current spec's scope)
mock_model('Part').as_new_ record.as_null_object
}
before do
assign(:part, part) <---- :part refers to the part var in spec (@part in view), whilst 'part' at the end refers to just the part method in let.. Really stupid - i know.
end
it "renders a form to add a new part" do
render
render.should have_selector( "form",
:method => "post",
:action => parts_path,
) do |form|
form.should have_selector("input", :type => "submit")
end
end
it "renders input fields to add a new part" do
part.stub("title"=> "HKS EVC boost controller")
render
rendered.should have_selector( "form" ) do |form|
form.should have_selector( "input",
:type => "text",
:name => "part[title]",
:value => "HKS EVC boost controller"
)
end
end
end
I'm awaiting for some clarification on this. Will be interesting to post back what's going on...