[rspec-users] Unable to stub a class method in a before_filter

Fernando Perez lists at ruby-forum.com
Tue Nov 4 14:38:51 EST 2008

Here is my spec:
describe Admin::ProductsController, "A visitor wants to access
admin/products" do

  before(:each) do
    @product = mock_model(Product, :traffic_available => 0)

  it "should redirect to login path" do
    get :index
    response.should redirect_to(login_url)


In my application.rb, I have the following before filter:
  def find_current_site
    @site = Site.find_by_domain_name(website_name)
    if @site.traffic_available < 0
      render :nothing => :true

As the test DB is empty and I don't use fixtures, it won't find any
site, therefore nil.traffic_available < 0 returns an error when running
the spec.

I thought I could stub out the find_by_domain_name method to return a
cooperative object with a traffic_available value, but it didn't work.

How can I do that correctly?

I have found a dirty workaround:

but I don't find it acceptable. I am not going to stub out all methods
in my app, that's nonsense.
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list