Project

gruf-rspec

0.03
The project is in a healthy, maintained state
RSpec assistance library for gruf, including testing helpers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Dependencies

Development

Runtime

~> 2.5, >= 2.5.1
>= 3.8
 Project Readme

gruf-rspec

CircleCI Gem Version Documentation

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

Installation

gem 'gruf-rspec'

Then add the following code to your spec_helper.rb:

require 'gruf/rspec'

Note that this gem requires at least Ruby 2.6+, 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:

class ThingController < Gruf::Controllers::Base
  bind ::Rpc::Things::Service
  
  def get_thing
    Rpc::GetThingResponse.new(id: request.message.id)
  end
end

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

describe ThingController do
  describe '.get_thing' do
    let(:request_proto) { Rpc::GetThingRequest.new(id: rand(1..100)) }
    let(:metadata) {
      { 'user_id' => 'axj42i' }
    }

    subject { run_rpc(:GetThing, request_proto, active_call_options: { metadata: metadata }) }

    it 'will return the thing' do
      expect(subject).to be_a(Rpc::GetThingResponse)
      expect(subject.id).to eq request_proto.id
    end
  end
end

Alternatively, you can pass a block:

it 'will return the thing' do
  run_rpc(:GetThing, request_proto) do |resp|
    expect(resp).to be_a(Rpc::GetThingResponse)
    expect(resp.id).to eq request_proto.id
  end
end

Accessing the Bound Service

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

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

Matching Errors

You can match against errors as well:

describe 'testing an error' do
  let(:request_proto) { Rpc::GetThingRequest.new(id: rand(1..100)) }
  
  subject { run_rpc(:GetThing, request_proto) }
  
  it 'should fail with the appropriate error' do
    expect { subject }.to raise_rpc_error(GRPC::InvalidArgument)
  end
end

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

it 'should fail with the appropriate error code' do
  expect { subject }.to raise_rpc_error(GRPC::InvalidArgument).with_serialized { |err|
    expect(err).to be_a(MyCustomErrorClass)
    expect(err.error_code).to eq 'invalid_request'
  
    fe = err.field_errors.first
    expect(fe.field_name).to eq 'name'
    expect(fe.error_code).to eq 'invalid_name'
    expect(fe.error_message).to eq 
  }
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 confiugration block like so:

Gruf::Rspec.configure do |c|
  c.rpc_spec_path = '/spec/rpc_controllers'
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:

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:

Dotenv.load # assuming the .env file has the RPC_SPEC_PATH var set
# or:
ENV['RPC_SPEC_PATH'] = '/spec/rpc_controllers'
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.