У меня есть миксин, который использует RestClient
:
module Restable
def get(url, options={})
response = RestClient::Request.new(
:method => :get,
:url => url,
:user => options[:user],
:password => options[:password],
:headers => {
:accept => :json,
:content_type => :json
}
).execute
response
end # /get
end # /Restable
Я хотел бы проверить возможность использования миксина с действительными и недействительными учетными данными.
I 'Мне трудно издеваться над RestClient
.Этот подход генерирует ошибки (warning: previous definition of RestClient was here
):
require 'spec_helper'
require File.expand_path('../../../lib/restable', __FILE__)
Describe Restable do
# create dummy object w/ mixin
before(:each) do
@test_obj = Object.new
@test_obj.extend(Restable)
end
url = 'http://domain.tld/'
context "get" do
it "an anonymous GET request returns success" do
RestClient = double
client = double
client.stub(:code) { 200 }
RestClient.stub(:get) { client }
response = @test_obj.get url
expect(response.code).to eq(200)
end
it "a GET request returns success (200) when valid credentials are supplied to a protected resource" do
RestClient = double
client = double
client.stub(:code) { 200 }
RestClient.stub(:get) { client }
url = 'http://domain.tld/private'
options = {user: 'valid_user', password: 'valid_password'}
response = @test_obj.get url, options
expect(response.code).to eq(200)
end
it "a GET request returns FORBIDDEN (403) when invalid credentials are supplied to a protected resource" do
RestClient = double
client = double
client.stub(:code) { 403 }
RestClient.stub(:get) { client }
url = 'http://domain.tld/private'
response = @test_obj.get url, {}
expect(response.code).to eq(403)
end
end # /context
end # /describe
Что я делаю не так?