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

Matt Wynne matt at mattwynne.net
Tue Nov 4 14:51:19 EST 2008


On 4 Nov 2008, at 19:38, Fernando Perez wrote:
> 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)
>    Product.stub!(:find_by_domain_name).and_return(@product)

Should this be Site.stub!(:find_by_domain_name)... ?

>
>  end
>
>  it "should redirect to login path" do
>    get :index
>    response.should redirect_to(login_url)
>  end
>
> end
> --
>
> 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
>    end
>  end
> --
>
> 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.




More information about the rspec-users mailing list