Rspec: переменная область видимости между shared_examples и shared_context

У меня возникли проблемы с параметризацией shared_example при тестировании моего контроллера rspec. Мой код основан на примерах передачи в общую группу, как описано в Relish

На первый взгляд кажется, что переменные экземпляра, созданные в shared_context, недоступны в shared_examples. Любые идеи?

Я использую Rails 3.1 с драгоценными камнями CanCan и Devise.

Контроллер (app/controllers/stores_controller.rb):

class StoresController < SubDomainController
  def index
    authorize! :list, Store
    @stores = current_user.company.stores.allrespond_to do |format|
      format.html # index.html.erb
      format.json { render json: @stores }
    end
  end
end

Общий модуль (spec/support/shared.rb):

  module Shared
    shared_context "stores controller" do    
      before :all do    
        build_companies
        build_stores
        build_users
      end
    end

    shared_context "admin logged in" do   
      before :each do
        login_admin
      end    
    end

    #########THIS IS THE TEST THAT IS FAILING#########
    shared_examples "authorised_user" do |values|

      it "should assign correct variables" do
        values.each do |key,val|
          assigns[key].should == values[key]
        end
      end
    end
  end

Модуль помощников (spec/support/helpers.rb):

module Helpers

  def build_companies
    @company = build_company(get_rand_string() ,"Correct")
    @other_company = build_company(get_rand_string() ,"Invalid")
  end

  def build_stores
    @store = FactoryGirl.create(:store, :company_id => @company.id)
    @other_store = FactoryGirl.create(:store, :company_id => @company.id)
    @other_company_store = FactoryGirl.create(:store, :company_id => @other_company.id)
  end

  def build_users
    @admin_user = FactoryGirl.create(:user, :role_id => 1,  :company => @company, :store_id => @store.id)
    @manager_user = FactoryGirl.create(:user, :role_id=> 2,  :company => @company, :store_id => @store.id)
    @regular_user = FactoryGirl.create(:user, :role_id=> 3,  :company => @company, :store_id => @store.id)
  end       
end

Помощники спецификаций (spec/spec_helper.rb):

.....
  config.include  Helpers
  config.include  Shared
.... 

Спецификация контроллера (spec/controllers/stores_controller_spec.rb): требуется «spec_helper»

describe StoresController do    
  include_context "stores controller"
    describe "INDEX" do

        [:admin].each do |u|
          describe "#{u} authorised" do          
            include_context "#{u} logged in"          
              before :each do
                get :index
              end

              ###passing instance variables to Helper module not working######
              it_behaves_like "authorised_user", {:stores => [@store, @other_store]}      

            end
          end
        end
    end 
end

Ошибки:

1) StoresController INDEX company stores admin authorised behaves like authorised_user should assign correct variables
   Failure/Error: assigns[key].should == values[key]
   expected: [nil, nil]
        got: [#<Store id: 1, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">, #<Store id: 2, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">] (using ==)                                                                                                                         
   Diff:
   @@ -1,2 +1,3 @@
   -[nil, nil]
   +[#<Store id: 1, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">,                                                                                                                                             
   + #<Store id: 2, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">]

person cash22    schedule 18.06.2012    source источник


Ответы (1)


Разобрался с этим сам.

Переменная должна быть установлена ​​с использованием синтаксиса «let» в store_controller_spec.rb следующим образом.

it_behaves_like "authorised_user" do
   let(:variable){stores}
   let(:values){[@store, @other_store]} 
end

Затем в файле shared_examples.rb:

shared_examples "authorised_user" do

  it "should assign correct variables" do
      assigns[variable].should == values
  end
end
person cash22    schedule 26.06.2012