[rspec-users] Specing with Subdomains as Account Keys

Ryan Heneise lists at artofmission.com
Fri Oct 26 13:27:38 EDT 2007


On Oct 26, 2007, at 12:11 PM, sinclair bain wrote:

> Ryan,
>
> Are you stubbing
>
> @current_company = Company.find_by_subdomain(account_subdomain)
>
>  in the inherited  #find_current_company method ?
>
> Make sure the @current_company is the same one upon which you have  
> your expectations ?
> ie
>
> before do
>  ...
>  Company.stub!(:find_by_subdomain).and_return(@current_company)
>
>
> end
>
> A pint and a bag of crisps says that does it !  ; )
>
> Cheers!
> sinclair


Absolutely smashing! It worked! Here's my setup now:

before do
   @item = mock_model(Item)
   @current_company = mock_model(Company)
   Company.stub!(:find_by_subdomain).and_return(@current_company)
   @current_company.stub!(:find_items).with("1").and_return(@item)
end

What's your address - I'll send you a pint and a bag of crisps :)

Ryan








> On 10/26/07, Ryan Heneise < lists at artofmission.com> wrote:How do  
> you go about implementing and rspecing subdomains as account
> keys? I'm sure that this must be an issues for others as well.
>
> So I have an app using subdomains as account keys. The Application
> Controller sets up @current_company in a before filter. Everything is
> done within the context of the @current_company.
>
> After reading about Demeter's Revenge (http://www.lukeredpath.co.uk/
> 2007/10/18/demeters-revenge), and reading Sinclair's advice (link
> above), I refactored @current_company.items.find(:all) to
> @current_company.find_items.
>
> Still no luck. The spec fails with:
> -- Mock 'Company' expected :find_items with (:all) once, but received
> it 0 times
>
> The other thing that bugs me is the size of the setup method. It's
> getting too long and starting to smell. Obviously I must be making
> this harder on myself that it needs to be.
>
> Any ideas?
>
> (This question was originally posted under "Mocking/Stubbing help
> with subdomain as account key"
> - http://rubyforge.org/pipermail/rspec-users/2007-October/
> 004138.html, but I've rephrased it to make it more clear with the
> subdomain as account key question.)
>
>
> # items_controller_spec.rb
> describe ItemsController, "handling GET /items" do
>    fixtures :companies
>
>    before do
>      @request.host = "subdomain.test.host"
>      @item = mock(Item)
>      Item.stub!(:find).and_return([@item])
>      @current_company = mock(Company)
>      @current_company.stub!(:items).and_return([])
>      @current_company.stub!(:find_items).and_return([@item])
>      @current_company.stub!(:subdomain).and_return("subdomain")
>    end
>
>     # Passes
>    def do_get
>      get :index
>    end
>
>     # Passes
>    it "should be successful" do
>      do_get
>      response.should be_success
>    end
>
>     # Passes
>    it "should render index template" do
>      do_get
>      response.should render_template('index')
>    end
>
>    # FAILS with message:
>    # Mock 'Company' expected :find_items with (:all) once, but
> received it 0 times
>    it "should find all items" do
>      @current_company.should_receive(:find_items).with
> (:all).and_return([@item])
>      do_get
>    end
>
>    # Passes
>    it "should assign the found items for the view" do
>      do_get
>      assigns[:items].should == [@item]
>    end
> end
>
>
> # items_controller.rb
> class ItemsController < ApplicationController
>    def index
>      @items = @current_company.find_items(:all)
>    end
> end
>
>
> # company.rb
> class Company < ActiveRecord::Base
>    has_many :items
>    def find_items(*args)
>      items.find(*args)
>    end
> end
>
>
> # application.rb
> class ApplicationController < ActionController::Base
>    before_filter :find_current_company
>    def find_current_company
>      @current_company = Company.find_by_subdomain(account_subdomain)
>    end
> end
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
>
>
> -- 
>
>
> Cheers!
> sinclair
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list