项目作者: bigcommerce

项目描述 :
RSpec helper suite for gruf
高级语言: Ruby
项目地址: git://github.com/bigcommerce/gruf-rspec.git
创建时间: 2018-11-12T22:58:31Z
项目社区:https://github.com/bigcommerce/gruf-rspec

开源协议:MIT License

下载


gruf-rspec

CircleCI Gem Version Documentation Maintainability Test Coverage

Assistance helpers and custom type for easy testing Gruf controllers with
RSpec.

Installation

  1. gem 'gruf-rspec'

Note that this gem requires at least Ruby 3.0+, Gruf 2.5.1+, and RSpec 3.8+.

Usage

  • Add a test for a Gruf controller in spec/rpc
  • Run the run_rpc method with three args: The gRPC method name, the request object
    and the active_call_options. The third argument is optional.
  • Validate the response

Example

Let’s assume you have a gruf controller named ThingController that is bound to the gRPC
service Rpc::Things::Service. That has a method GetThing:

  1. class ThingController < Gruf::Controllers::Base
  2. bind ::Rpc::Things::Service
  3. def get_thing
  4. Rpc::GetThingResponse.new(id: request.message.id)
  5. end
  6. end

To test it, you’d create spec/rpc/thing_controller_spec.rb:

  1. describe ThingController do
  2. describe '#get_thing' do
  3. let(:request_proto) { Rpc::GetThingRequest.new(id: rand(1..100)) }
  4. let(:metadata) {
  5. { 'user_id' => 'axj42i' }
  6. }
  7. subject { run_rpc(:GetThing, request_proto, active_call_options: { metadata: metadata }) }
  8. it 'returns the thing' do
  9. expect(subject).to be_a_successful_rpc
  10. expect(subject.id).to eq request_proto.id
  11. end
  12. end
  13. end

Alternatively, you can pass a block:

  1. it 'returns the thing' do
  2. run_rpc(:GetThing, request_proto) do |resp|
  3. expect(resp).to be_a_successful_rpc
  4. expect(resp.id).to eq request_proto.id
  5. end
  6. end

Accessing the Bound Service

Note that you can also access the bound gRPC service class:

  1. it 'binds the service correctly' do
  2. expect(grpc_bound_service).to eq Rpc::Things::Service
  3. end

Matching Errors

You can match against errors as well:

  1. describe 'testing an error' do
  2. let(:request_proto) { Rpc::GetThingRequest.new(id: rand(1..100)) }
  3. subject { run_rpc(:GetThing, request_proto) }
  4. it 'fails with the appropriate error' do
  5. expect { subject }.to raise_rpc_error(GRPC::InvalidArgument)
  6. end
  7. end

Or further, even check your serialized error that is passed in metadata:

  1. it 'fails with the appropriate error code' do
  2. expect { subject }.to raise_rpc_error(GRPC::InvalidArgument).with_serialized { |err|
  3. expect(err).to be_a(MyCustomErrorClass)
  4. expect(err.error_code).to eq 'invalid_request'
  5. fe = err.field_errors.first
  6. expect(fe.field_name).to eq 'name'
  7. expect(fe.error_code).to eq 'invalid_name'
  8. expect(fe.error_message).to eq 'That name is already taken!'
  9. }
  10. end

Note that when using with_serialized, you must pass the block with { }, not using
do and end.

RSpec Controller Matcher Configuration

By default, the type matcher for Gruf controllers matches in /spec/rpc. You can customize this by configuring it
in the Gruf::Rspec configuration block like so:

  1. Gruf::Rspec.configure do |c|
  2. c.rpc_spec_path = '/spec/rpc_controllers'
  3. end

Alternatively, you can pass configuration of the path via ENV. For example, where
RPC_SPEC_PATH="/spec/rpc_controllers" is set in a .env file:

  1. bundle exec dotenv rspec

Or, add require: false to the gemspec for the gruf-rspec gem, and then explicitly require it after setting the ENV
var:

  1. Dotenv.load # assuming the .env file has the RPC_SPEC_PATH var set
  2. # or:
  3. ENV['RPC_SPEC_PATH'] = '/spec/rpc_controllers'
  4. require 'gruf/rspec'

License

Copyright (c) 2018-present, BigCommerce Pty. Ltd. All rights reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.