Skip to content
Library for stubbing gRPC requests in Ruby.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin rubocop -a Jan 7, 2019
lib
spec
.gitignore
.rspec bundle gem Jul 6, 2018
.rubocop.yml rubocop -a Jan 7, 2019
.travis.yml
Gemfile
LICENSE.txt
README.md Update misc and support ruby 2.5 Jan 7, 2019
Rakefile
grpc_mock.gemspec

README.md

GrpcMock Build Status

Library for stubbing grpc in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'grpc_mock'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grpc_mock

Usage

If you use RSpec, add the following code to spec/spec_helper.rb:

require 'grpc_mock/rspec'

Examples

See definition of protocol buffers and gRPC generated code in spec/exmaples/hello

Stubbed request based on path and with the default response
GrpcMock.stub_request("/hello.hello/Hello").to_return(Hello::HelloResponse.new(msg: 'test'))

client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
client = client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
Stubbing requests based on path and request
GrpcMock.stub_request("/hello.hello/Hello").with(Hello::HelloRequest.new(msg: 'hi')).to_return(Hello::HelloResponse.new(msg: 'test'))

client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => send a request to server
client = client.hello(Hello::HelloRequest.new(msg: 'hi'))    # => Hello::HelloResponse.new(msg: 'test') (without any requests to server)
Real requests to network can be allowed or disabled
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)

GrpcMock.disable_net_connect!
client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error

GrpcMock.allow_net_connect!
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ganmacs/grpc_mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.