2012-03-09 3 views
0

나는 다음과 같은 간단한 클래스와 HTTParty이 방법RSpec으로이 코드를 테스트하는 방법은 무엇입니까?

class Token 
    require 'httparty' 

    include HTTParty 
    base_uri 'https://<some url>' 
    headers 'auth_user' => 'user' 
    headers 'auth_pass' => 'password' 
    headers 'auth_appkey' => 'app_key' 

    def self.getToken 
    response = get('/auth/token') 
    @token = response['auth']['token'] 
    end 
end 

내가 레일 콘솔에서 메서드를 호출하고 성공적으로 다시 토큰을 얻을 수 있기 때문에 작동 알고있다.

RSpec에서 위의 코드를 테스트하려면 어떻게해야합니까? 그것에

내 초기 자상이 작동하지 않습니다

describe Token do 
    before do 
    HTTParty.base_uri 'https://<some url>' 
    HTTParty.headers 'auth_user' => 'user' 
    HTTParty.headers 'auth_pass' => 'password' 
    HTTParty.headers 'auth_appkey' => 'app_key' 
    end 

    it "gets a token" do 
    HTTParty.get('auth/authenticate') 
    response['auth']['token'].should_not be_nil 
    end 
end 

그것은 말한다 : NoMethodError: undefined method 'base_uri' for HTTParty:Module ...

감사합니다!

describe Token do 
    before do 
     @a_class = Class.new do 
     include HTTParty 
     base_uri 'https://<some url>' 
     headers 'auth_user' => 'user' 
     headers 'auth_pass' => 'password' 
     headers 'auth_appkey' => 'app_key' 
     end 
    end 

    it "gets a token" do 
     response = @a_class.get('auth/authenticate') 
     response['auth']['token'].should_not be_nil 
    end 
end 

이 익명 클래스를 생성하고 HTTPparty의 클래스 메소드로 확장 : 당신이 모듈을 테스트하기 때문에

+0

서버 또는이 (매우 얇은) 클라이언트를 테스트하려면 어떻게해야합니까? –

+0

클라이언트를 테스트하고 싶습니다. 이것은 웹 서비스를 사용하기 위해 작성한 첫 번째 방법 일뿐입니다. 더 추가하기 전에 테스트를 작성하고 싶지만 사용할 구문을 모른다. –

답변

1

이 뭔가를 시도 할 수 있습니다. 그러나 응답을받을 때 확신 할 수는 없습니다.

+0

테스트를 통과 한 작은 수정을했습니다. 당신의 도움을 주셔서 감사합니다! –

관련 문제