2012-06-02 3 views
2

내 모든 루비 레일 앱에서 퍼시스턴스 클래스와 독립적이어야하므로 컨트롤러에서 데이터베이스를 사용하지 않습니다. 나는 조롱을 대신 사용했다.레일스에서 ​​스코프 체인을 테스트하는 가장 좋은 방법

class CouponsController < ApplicationController 
    def index 
    @coupons = Coupon.all 
    end 
end 

require 'spec_helper' 
describe CouponsController do 
    let(:all_coupons) { mock } 
    it 'should return all coupons' do 
    Coupon.should_receive(:all).and_return(all_coupons) 
    get :index 
    assigns(:coupons).should == all_coupons 
    response.should be_success 
    end 
end 

그러나 컨트롤러와 같은 더 복잡한 범위를 포함하는 경우 무엇을 :

class CouponsController < ApplicationController 
    def index 
    @coupons = Coupon.unredeemed.by_shop(shop).by_country(country) 
    end 
end 

당신이 simillar 범위를 테스트하기위한 좋은 방법을 알고 계십니까 여기

은 RSpec에와 RSpec에-모의에 대한 예입니다 쇠사슬?

나는 다음과 같은 시험은 정말 잘 보이지 않는 생각 : 당신은 그것에 대해 stub_chain 방법을 사용할 수 있습니다

require 'spec_helper' 
describe CouponsController do 
    it 'should return all coupons' do 
    Coupon.should_receive(:unredeemed).and_return(result = mock) 
    result.should_receive(:by_shop).with(shop).and_return(result) 
    result.should_receive(:by_country).with(country).and_return(result) 
    get :index 
    assigns(:coupons).should == result 
    response.should be_success 
    end 
end 

답변

6

. 같은

뭔가 :

Coupon.stub_chain(:unredeemed, :by_shop, :by_country).and_return(result) 

그냥 예. rspec > 3 사용이 구문

+0

나는 그것에 대해 전에 결코 알지 못한다. 고마워 :) – Chamnap

+1

그래,하지만 누군가가이 코드를 삭제하고 다른 종류의 메소드를 사용하여 결과를 얻으면 stub_chain을 사용하면 실패하지 않을 것이다. – Konrad

0

:

expect(Converter).to receive_message_chain("new.update_value").with('test').with(no_args) 

대신 stub_chain

.

메시지 체인에 대한 추가 정보는 in the documenation입니다.

관련 문제