Fernando Perez
2008-Nov-04 19:38 UTC
[rspec-users] Unable to stub a class method in a before_filter
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) 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. How can I do that correctly? I have found a dirty workaround: controller.stub!(:find_current_site).and_return(:true) 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/.
David Chelimsky
2008-Nov-04 19:45 UTC
[rspec-users] Unable to stub a class method in a before_filter
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) > Product.stub!(:find_by_domain_name).and_return(@product) > 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. > > How can I do that correctly? > > I have found a dirty workaround: > controller.stub!(:find_current_site).and_return(:true) > > 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 controller.stub!(:find_current_site).and_return(:true) end end That work?> -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Matt Wynne
2008-Nov-04 19:51 UTC
[rspec-users] Unable to stub a class method in a before_filter
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.
Fernando Perez
2008-Nov-04 20:42 UTC
[rspec-users] Unable to stub a class method in a before_filter
> That work?It could. But in such case, how will I test my before_filter independently? My real problem is to stub the find_by_domain_name instance method. -- Posted via http://www.ruby-forum.com/.
Fernando Perez
2008-Nov-04 20:46 UTC
[rspec-users] Unable to stub a class method in a before_filter
Are the docs on mock outdated? I sometimes see mock_model, and sometimes mock. Which one should be used? -- Posted via http://www.ruby-forum.com/.
Fernando Perez
2008-Nov-04 20:51 UTC
[rspec-users] Unable to stub a class method in a before_filter
Crap! I was stubbing Product, instead of Site! It all works now. Matt''s typo put me on the track! But I will definitely use the trick Spec::Runner.configure do |config|... to avoid having to type this stub in all my files. Thanks. -- Posted via http://www.ruby-forum.com/.
Scott Taylor
2008-Nov-04 20:55 UTC
[rspec-users] Unable to stub a class method in a before_filter
On Nov 4, 2008, at 3:46 PM, Fernando Perez wrote:> Are the docs on mock outdated? I sometimes see mock_model, and > sometimes > mock. Which one should be used?mock_model is for ActiveRecord objects. it''s just a mock() call with a random id stub set, and a :new_record? => false Scott
Matt Wynne
2008-Nov-04 20:56 UTC
[rspec-users] Unable to stub a class method in a before_filter
On 4 Nov 2008, at 20:46, Fernando Perez wrote:> Are the docs on mock outdated? I sometimes see mock_model, and > sometimes > mock. Which one should be used?For mock_model, you need to look at the rspec-rails gem, which is a separate library. You actually have three choices when mocking an ActiveRecord model: mock, mock_model and stub_model. mock() belongs to rspec''s core library, while the latter two come with the rails-specific extension gem. See here for the documentation on the two rails mocking methods: http://rspec.rubyforge.org/rspec-rails/1.1.11/classes/Spec/Rails/Mocks.html#M000026 There''s also a good blog post on David''s blog about stub_model http://blog.davidchelimsky.net/2008/5/27/rspec-1-1-4 HTH, Matt