Code Monkey home page Code Monkey logo

rspec-request_describer's Introduction

RSpec::RequestDescriber

test Gem Version

RSpec plugin to write self-documenting request-specs.

This gem is designed for:

Setup

Install

Add this line to your application's Gemfile:

gem 'rspec-request_describer'

And then execute:

bundle

Or install it yourself as:

gem install rspec-request_describer

Include

Include RSpec::RequestDescriber to your example groups like this:

require 'rspec/request_describer'

RSpec.configure do |config|
  config.include RSpec::RequestDescriber, type: :request
end

Usage

Note that this is an example in a Rails app.

subject

RSpec::RequestDescriber provides subject from its top-level description.

# subject will be `get('/users')`.
RSpec.describe 'GET /users' do
  it 'returns 200' do
    subject
    expect(response).to have_http_status(200)
  end
end

headers

If you want to modify request headers, change headers before calling subject.

# `subject` will be `get('/users', headers: { 'Authorization' => 'token 12345' })`.
RSpec.describe 'GET /users' do
  context 'with Authorization header' do
    before do
      headers['Authorization'] = 'token 12345'
    end

    it 'returns 200' do
      subject
      expect(response).to have_http_status(200)
    end
  end
end

params

If you want to modify request parameters, change params before calling subject.

# `subject` will be `get('/users', params: { 'sort' => 'id' })`.
RSpec.describe 'GET /users' do
  context 'with sort parameter' do
    before do
      params['sort'] = 'id'
    end

    it 'returns 200 with expected JSON body' do
      subject
      expect(response).to have_http_status(200)
      expect(response.parsed_body).to match(
        [
          hash_including('id' => 1),
          hash_including('id' => 2),
        ]
      )
    end
  end
end

path parameters

You can embed variables in URL path like /users/:user_id. In this example, the returned value of user_id method will be embedded as its real value.

# `subject` will be `get("/users/#{user_id}")`.
RSpec.describe 'GET /users/:user_id' do
  let(:user) do
    User.create(name: 'alice')
  end

  let(:user_id) do
    user.id
  end

  it 'returns 200' do
    subject
    expect(response).to have_http_status(200)
  end
end

rspec-request_describer's People

Contributors

dependabot[bot] avatar gongo avatar hanachin avatar hiroki-uchida avatar k5trismegistus avatar mrkn avatar onk avatar pocke avatar r7kamura avatar wata727 avatar yujinakayama avatar yuku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rspec-request_describer's Issues

Must explicit invoke `should`?

Hi,

I found that if I'm using

it { should == 200 }

it will works. But if I'm using

it "...." do
  expect(JSON.parse(response.body)["username"]).to eq("lazywei")
end

then it will raise error that response is nil.

I'll need to invoke the subject explicitly before using response

it "...." do
  should == 200
  expect(JSON.parse(response.body)["username"]).to eq("lazywei")
end

I don't think this "implicity" is good design. IMHO, I'd prefer I can call post ... or something like do_request explicitly by myself. This makes the test case more readable.
How do you think?

Thanks.

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.