Below are two methods of doing the same thing in a view spec.  I have some 
hidden magic to tie in to capybara which I am not showing.

To me, the advantage of the first is if there is a failure, there will be a 
very nice error message that tells you exactly what is wrong.  The downside of 
the first is it renders the template eight times instead of just once.  I can't 
figure out a way around that.  "render" is not available until after the "it" 
is entered.  before(:all) didn't work when I tried it.  The first form also 
creates beautiful documentation when --format documentation is used.

Part of my question is this: I've heard that with unit tests, you want one 
"assert" per test.  This is what the first method does.

The second method is very concise... but does not produce as nice an error 
message and rather horrible output for --format documentation

I'm also curious if rigorously testing a form like this is typical.  I know 
that I often screw up the forms and don't figure it out until I try to exercise 
them.

describe "welcome/index.html.erb" do
  # first method
  describe "the swinfo form" do
    before(:each) do
      render
    end

    def form
      @form ||= rendered.find('form#swinfo_form')
    end

    def label
      @label ||= form.find('label')
    end

    def text_box
      @text_box ||= form.find('input[type="text"]')
    end

    it "should exist" do
      form
    end

    it "should have an action of /swinfos" do
      form[:action].should == '/swinfos'
    end

    it "shoule have a method of post" do
      form[:method].should == 'post'
    end

    it "should have a label" do
      label
    end

    describe "the label" do
      it "should have text of 'swinfo'" do
        label.text.should == 'swinfo'
      end

      it "should be for 'item'" do
        label[:for].should == 'item'
      end
    end

    it 'should have a text field' do
      text_box
    end

    describe "the text field" do
      it "should have a name of 'item'" do
        text_box[:name].should == 'item'
      end
    end
  end

  # second method
  it "the 'which filesets' form" do
    render
    within 'form#which_filesets_form' do |form|
      form[:action].should == '/which_filesets'
      form.should have_selector('input[type="submit"]')

      form.find('label').tap do |label|
        label.text.should == 'which fileset'
        label[:for].should == 'path'
      end

      form.find('input[type="text"]').tap do |text_box|
        text_box[:name].should == 'path'
      end
    end
  end
end


-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to