8

나는 페이스 북 스타일 알림을 위해 푸셔를 사용하고 있습니다. 푸시 버튼이 트리거되었는지 테스트하기 위해 간단한 RSpec 테스트를 설정했습니다.RSpec을 사용하여 푸셔를 테스트하는 방법

scenario "new comment should notify post creator" do 
    sign_in_as(user) 
    visit user_path(poster) 
    fill_in "comment_content", :with => "Great Post!" 
    click_button "Submit" 

    client = double 
    Pusher.stub(:[]).with("User-1").and_return(client) 
    client.should_receive(:trigger) 
end 

이 테스트는 통과합니다. 그러나 동일한 코드를 사용하여 다른 테스트를 한 경우 (두 번 똑같은 테스트) 두 번째 테스트는 통과되지 않습니다. 두 번째 테스트를 동일한 파일이나 다른 파일에 넣어도 상관 없습니다. 필자는 본질적으로 Pusher를 한 번만 테스트 할 수 있습니다.

두 번째 테스트는 위해 내가 오류 ...

Failure/Error: client.should_receive(:trigger) 
    (Double).trigger(any args) 
    expected: 1 time with any arguments 
    received: 0 times with any arguments 

답변

0

이것은 오래된 질문이 될 수도 있지만 나는 내 대답을 추가하고 싶었다. 우리는 또한 개발 및 https://github.com/tristandunn/pusher-fake에서 사용 가능한 테스트하기위한 가짜 푸셔 서버입니다 미는 가짜를 사용

it "user can publish the question" do 
    expect_any_instance_of(Pusher::Client).to receive(:trigger) 
    visit event_path(event) 
    click_on 'Push Question to Audience' 
    expect(current_path).to eq question_path(@question) 
    expect(page).to have_content 'Question has been pushed to the audience' 
end 

: 레일 응용 프로그램에서 이전에 RSpec에와 푸셔를 테스트 할 때 다음과 같이 우리는 기능 사양을 썼다.

"실행하면 임의의 열린 포트 두 개에서 가짜 서비스가 시작됩니다. 푸시 계정 없이도 서비스에 연결할 수 있습니다. 소켓 및 웹 서버의 호스트 및 포트는 구성. "

require "rails_helper" 

feature "Server triggering a message" do 
    before do 
    connect 
    connect_as "Bob" 
    end 

    scenario "triggers a message on the chat channel", js: true do 
    trigger "chat", "message", body: "Hello, world!" 

    expect(page).to have_content("Hello, world!") 

    using_session("Bob") do 
     expect(page).to have_content("Hello, world!") 
    end 
    end 

    protected 

    def trigger(channel, event, data) 
    Pusher.trigger(channel, event, data) 
    end 
end 

이 방법을 보여주는 예제의 repo 찾을 수 https://github.com/tristandunn/pusher-fake-example

에서 :이 다음을 수행 할 수 있습니다