Coder Social home page Coder Social logo

grpc_mock's Introduction

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/examples/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.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.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)

Responding dynamically to the stubbed requests

GrpcMock.stub_request("/hello.hello/Hello").to_return do |req, call|
  Hello::HelloResponse.new(msg: "#{req.msg} too")
end

client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
client.hello(Hello::HelloRequest.new(msg: 'hi'))    # => Hello::HelloResponse.new(msg: 'hi too')

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.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error

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

Raising errors

Exception declared by class

GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError)

client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Raise StandardError

or by exception instance

GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError.new("Some error"))

or by string

GrpcMock.stub_request("/hello.hello/Hello").to_raise("Some error")

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.

grpc_mock's People

Contributors

ganmacs avatar qnighy avatar s4ichi avatar asux avatar amatsuda avatar eisuke avatar hatstand avatar magec avatar bagmangood avatar luccafort avatar yeonhoyoon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.