[rspec-users] Unable to stub a class method in a before_filter
dchelimsky at gmail.com
Tue Nov 4 14:45:28 EST 2008
On Tue, Nov 4, 2008 at 1:38 PM, Fernando Perez <lists at ruby-forum.com> 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)
> 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.
I don't know if this will satisfy you or not, but you *could* write
that once and have it apply across your entire suite. Add this to
spec/spec_helper.rb (or equivalent):
Spec::Runner.configure do |config|
config.before(:each, :type => :controller) do
> Posted via http://www.ruby-forum.com/.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users