Skip to content

Context variables

David edited this page Mar 21, 2015 · 1 revision

Vigia tries to be consistent with the way the RSpec are normally written. It creates the describe groups and context based on the adapter configuration and set up all the variables for the examples by using RSpec memoized objects.

# With an adapter `ExampleAdapter` with this config
#
# group :resource,
#   describes: [ :posts, :pages ],
#   children:  [ :action ]
#
# group :action
#   describes: [ :get, :post ],
#   context:   [ :default ]
#
# context :default,
#   http_client_options: {
#     method:  -> { action.to_s.upcase }
#     url:     -> { adapter.url_for(resource) }" }
#     headers: :headers
#   expectations:
#     code: :code
#     headers: {}
#     body: -> { Body.for(resource} }
#
# Vigia will generate this RSpec code

describe Vigia::RSpec do
  let(:adapter) { ExampleAdapter.instance }
  let(:client)  { Vigia.config.http_client_class.new(http_options) }
  let(:result)  { client.run }

  # the loop starts...
  describe 'posts' do
    let(:resource) { :posts }

    describe 'action' do
      let(:action) { :get }

      context 'default' do
        let(:http_client_options) { Vigia::HttpClient::Options.new(context_options) }
        let(:expectations)        { Vigia::HttpClient::ExpectedRequest.new(expectations) }

        # EXAMPLES RUN HERE!
      end
      # NEXT ACTION
    end
    # NEXT RESOURCE
  end
end

Also, It is important to mention that it is in this context where the adapter configuration will be executed. In the previous example, we configured the http_client option as follows:

#   http_client_options: {
#     method:  -> { action.to_s.upcase }
#     url:     -> { adapter.url_for(resource) }" }
#     headers: :headers

The option method is a lambda object. This object will be executed inside the RSpec memoized objects context. It is the same as doing:

  # it has access to all context/group memoized objects
  let(:method) { action.to_s.upcase }

You can also use the adapter like in option url, since it has been defined as a memoized object by Vigia::RSpec.

Lastly, you can specify a symbol as the option value. In this case, the adapter will be the reciever of this method.

Clone this wiki locally