2011-01-02 1 views
0

rspec 2를 처음 사용하면서 문제가 발생했다는 것을 알았습니다. 어쨌든 내 스텁 중 하나에 있습니다. 내 'clients_controller.rb'에서 'show'작업을 테스트하려고하는데 실패하지만 개체 대신 Enumerable :: Enumerator가 반환됩니다. '색인'작업은 정상적으로 작동합니다. 내 목표는 현재 로그인 한 사용자가 자신의 클라이언트를 볼 수 있지만 다른 사용자의 클라이언트는 볼 수 없도록하는 것입니다. 함께Rspec 스텁 된 파인더가 하나의 객체 대신에 열거 가능을 찾는다

before_filter :require_user 

def index 
    @clients = current_user.clients 
end 
def show 
    @client = current_user.clients.find(params[:id]) 
end 

:

before :each do 
    stub_current_user 
    @my_client = mock_model(Client, :user_id => @current_user.id) 
    @not_my_client = mock_model(Client, :user_id => @current_user.id + 1) 
    @current_user.stub!(:clients) { @clients = [@my_client] } 
    @clients.stub!(:find){ @my_client } 
end 

describe "GET index" do 
    it "assigns the current user's clients as @clients" do 
    get :index 
    assigns(:clients).should eq([@my_client]) 
    end 
end 

describe "GET show" do 
    it "assigns the requested client as @client if @client is the current user's client" do 
    get :show, :id => @my_client.id 
    assigns(:client).should eq(@my_client) 
    end 

    it "does not assign the requested client if @client is not the current user's client" do 
    get :show, :id => @not_my_client.id 
    assigns(:client).should == nil 
    end 
end 

stub_current_user는 spec_helpers.rb이다 :

def stub_current_user 
    @current_user = mock_model(User, :name => 'Bob Johnson', :email => '[email protected]', 
            :role => "account_holder", :incomplete_subscription? => false, 
            :plan => mock_model(Plan, :client_limit => 5), 
            :monthly_rate => 100) 
    if defined?(controller) # for controller specs 
    controller.stub!(:current_user).and_return(@current_user) 
    elsif defined?(template) # for view specs 
    template.stub!(:current_user).and_return(@current_user) 
    else 
    'wat' 
    end 
end 

'인덱스'행동 테스트를 통과하지만, '표시'액션의 두 테스트가 실패 , 다음 오류와 함께 :

1) ClientsController GET show assigns the requested client as @client if @client is the current user's client 
Failure/Error: assigns(:client).should eq(@my_client) 

expected #<Client:0x81b67840 @name="Client_1007"> 
     got #<Enumerable::Enumerator:0x1036471d0> 

(compared using ==) 

Diff: 
@@ -1,2 +1,2 @@ 
-#<Client:0x81b67840 @name="Client_1007"> 
+#<Enumerable::Enumerator:0x1036471d0> 
# ./spec/controllers/clients_controller_spec.rb:31 

2) ClientsController GET show does not assigns the requested client if @client is not the current user's client 
Failure/Error: assigns(:client).should == nil 
expected: nil, 
     got: #<Enumerable::Enumerator:0x1035336b8> (using ==) 
# ./spec/controllers/clients_controller_spec.rb:39 

mock, tests 자체 또는 다른 곳에서 잘못 될지 확실하지 않습니다.

+0

'@current_user.id + 1 '로 기록이 존재 하는가 :

이 생각을 시도 할 수 있습니다? – Zabba

+0

@Zabba, 아니요. 나는 이전에 그것들을 스터핑하려고 시도했다 : 각각의 블록은 @other_user = mock_model (User, : id => @ current_user.id + 1)과 @not_my_client를'@not_my_client = mock_model (Client, : user_id => @ other_user.id)' 하지만 여전히 같은 오류가 발생했습니다. 포인터 주셔서 감사! – Will

답변

0

열거 형을 반환하는 이유는 Rails 3이 클라이언트 연결을 호출 할 때 모든 레코드를로드하지 않고 열거 형을 반환하기 때문에 메모리와 함께 더 효율적일 수 있기 때문입니다. 기본적으로 위조 된 데이터베이스 커서가 있습니다. 이 문제를 어떻게 해결할 지 확신 할 수 없지만 적어도 잘못된 점에 대한 질문에 대답해야합니다.

get :index 
assigns(:clients).to_a.should eq([@my_client])