在 mixin 中模拟 RestClient
Mock the RestClient in a mixin
我有一个使用 RestClient
:
的 mixin
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
我希望能够使用有效和无效凭据测试 mixin 的使用情况。
我很难嘲笑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
我做错了什么?
您可以使用 Webmock Gem 存根请求和 return 您想要的 body/status 代码。
- 将 webmock gem 添加到您的 gem 文件
gem webmock
- 将 webmock 添加到您的规范助手中
require 'webmock/rspec'
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
WebMock.stub_request(:get, url).to_return(status: [200, 'OK'], body: { foo: 'bar' }.to_json)
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
url = 'http://domain.tld/private'
WebMock.stub_request(:get, url).with(basic_auth: ['valid_user', 'valid_pass']).to_return(status: [200, 'OK'], body: { foo: 'bar' }.to_json)
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
url = 'http://domain.tld/private'
WebMock.stub_request(:get, url).with(basic_auth: ['valid_user', 'invalid_pass']).to_return(status: [403, 'OK'], body: { foo: 'bar' }.to_json)
options = {user: 'valid_user', password: 'invalid_password'}
response = @test_obj.get url, {}
expect(response.code).to eq(403)
end
end # /context
end # /describe
我有一个使用 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
我希望能够使用有效和无效凭据测试 mixin 的使用情况。
我很难嘲笑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
我做错了什么?
您可以使用 Webmock Gem 存根请求和 return 您想要的 body/status 代码。
- 将 webmock gem 添加到您的 gem 文件
gem webmock
- 将 webmock 添加到您的规范助手中
require 'webmock/rspec'
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
WebMock.stub_request(:get, url).to_return(status: [200, 'OK'], body: { foo: 'bar' }.to_json)
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
url = 'http://domain.tld/private'
WebMock.stub_request(:get, url).with(basic_auth: ['valid_user', 'valid_pass']).to_return(status: [200, 'OK'], body: { foo: 'bar' }.to_json)
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
url = 'http://domain.tld/private'
WebMock.stub_request(:get, url).with(basic_auth: ['valid_user', 'invalid_pass']).to_return(status: [403, 'OK'], body: { foo: 'bar' }.to_json)
options = {user: 'valid_user', password: 'invalid_password'}
response = @test_obj.get url, {}
expect(response.code).to eq(403)
end
end # /context
end # /describe