Fire Emblem
Fire Emblem

Reputation: 5961

Two questions about testing Rails controllers using RSpec

  1. How do you call a before_filter directly to test it's implementation? Let's say I want to write a spec for ApplicationController that contains a few before filters. What is the standard way to test them? Note, I don't want to test that they've been called - I actually just want to test their implementation once in a single spec.

  2. I like having "render_views" at the top of my controller specs, but when I use stubs, I get all sorts of strange errors as it tries to get the id or other attributes on the stub object. How do you deal with this so that you can make sure your views are correct while using stubs?

I use RSpec.

Upvotes: 1

Views: 189

Answers (1)

Thomas R. Koll
Thomas R. Koll

Reputation: 3139

Ad 1) Are the proper helpers? Then test them as helpers. If the are methods in you controller then they are private you can test them with the help of send.

require 'spec_helper' 
describe PostsController do
  it "private #scope should be Post.all" do
    @controller.send(:scope).should == Post.all
  end
end

Ad 2) I have render_views as well, saves me some time on testing the views. I don't use stubs but real objects from FactoryGirl or the like. The whole mocking of objects sucks when your objects change and it's a lot of extra work. Factories aren't. Of course, your tests will get slower, but you'll be testing deeper and wider.

Upvotes: 2

Related Questions