[rspec-users] View specs - best practices

Jason N. jason.nah at gmail.com
Thu Jan 6 18:27:23 EST 2011


Howdy,

I'm trying to test some mailer views (which is no different to normal
views in terms of specs)... ran into the following problem...

In adherence to the "one expectation per 'spec'"... I'm trying to
write the following

describe "auth/mailer/signup.html.erb" do
   before(:each) do
      @user = Factory.build(:user)
      assign(:user, @user)
      render
   end

   subject { rendered }

   it { should include(@user.email) }
   it { should...
   it { should...
end


Before long, if you keep this up, your spec goes from quick to very
slow. Turns out, calling render takes a bit of effort and the
before(:each) tells rspec to call render before each "it" call.

SO... is there a way to cache this, so that the rendered output is
available to each spec to avoid re-rendering? Each "it" item, really
describes expectations on the output, which doesn't change for every
run.

I could default back down to a single it, but that would defeat the
purpose of writing small tests.

I've tried before(:all), but rspec doesn't like it. It generates the
following error:

Failure/Error: render
     NoMethodError:
       undefined method `example_group' for nil:NilClass


Cheers,
Jason


More information about the rspec-users mailing list