[rspec-users] View specs - best practices

Evgeniy Dolzhenko dolzenko at gmail.com
Mon Jan 10 11:40:21 EST 2011


+1, interested in the solution as well. We have the same issue with 
controller specs/Shoulda, e.g.:

   describe 'GET google_apps (with invalid credentials)' do
     before do
       ...
       get :google_apps
     end

     it { should respond_with(:ok) }
     it { should render_template("sessions/new") }
     it { should set_the_flash.to(/Could not authorize you from Google 
Apps because/) }
   end

Will call the controller code 3 times without any particular reason to.
Probably some caching could be added in Shoulda?

On 1/7/2011 2:27 AM, Jason N. wrote:
> 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
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list