Module: Payload::Testing

Defined in:
lib/payload/testing.rb

Overview

Helper methods for stubbing and injecting dependencies into unit tests.

These methods are intended for rspec controller tests and require rspec-mocks to work as expected. During capybara tests, all dependencies are available as defined in config/dependencies.rb.

Instance Method Summary collapse

Instance Method Details

#build_containerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
# File 'lib/payload/testing.rb', line 80

def build_container
  Container.new
end

#dependenciesObject

Returns the current Container which will be injected into the test Rack session.

Returns:

  • the current Container which will be injected into the test Rack session.



75
76
77
# File 'lib/payload/testing.rb', line 75

def dependencies
  @request.env[:dependencies]
end

#modify_dependencies {|Container| ... } ⇒ Object

Convenience for injecting a modified container into the test rack session.

Yields:

  • (Container)

    the container to be modified. The block should return the new container.



69
70
71
# File 'lib/payload/testing.rb', line 69

def modify_dependencies
  @request.env[:dependencies] = yield(dependencies)
end

#setup_controller_request_and_responseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an empty Container in the fake Rack environment.



14
15
16
17
# File 'lib/payload/testing.rb', line 14

def setup_controller_request_and_response
  super
  @request.env[:dependencies] = build_container
end

#stub_factory(dependency) ⇒ RSpec::Mocks::TestDouble

Injects a stubbed factory into the test Container and returns it.

Parameters:

  • dependency (Symbol)

    the name of the factory to stub.

Returns:

  • (RSpec::Mocks::TestDouble)

    the stubbed factory, which can have stubs for ‘new` applied to it.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/payload/testing.rb', line 38

def stub_factory(dependency)
  dependencies[dependency]
rescue Payload::UndefinedDependencyError
  double("#{dependency} factory").tap do |factory|
    modify_dependencies do |dependencies|
      dependencies.service(dependency) do |config|
        factory
      end
    end
  end
end

#stub_factory_instance(dependency, attributes) ⇒ RSpec::Mocks::TestDouble

Finds or injects a stubbed factory into the test Container and stubs an instance to be created with the given attributes.

Parameters:

  • dependency (Symbol)

    the name of the factory to stub.

  • attributes (Hash)

    the expected attributes to build the instance.

Returns:

  • (RSpec::Mocks::TestDouble)

    the result of the factory, which can then have additional stubs or expectations applied to it.



26
27
28
29
30
31
# File 'lib/payload/testing.rb', line 26

def stub_factory_instance(dependency, attributes)
  factory = stub_factory(dependency)
  double(dependency.to_s).tap do |double|
    allow(factory).to receive(:new).with(attributes).and_return(double)
  end
end

#stub_service(dependency) ⇒ RSpec::Mocks::TestDouble

Injects a stubbed service into the test Container and returns it.

Parameters:

  • dependency (Symbol)

    the name of the service to stub.

Returns:

  • (RSpec::Mocks::TestDouble)

    the stubbed service, which can then have additional stubs or expectations applied to it.



55
56
57
58
59
60
61
62
63
# File 'lib/payload/testing.rb', line 55

def stub_service(dependency)
  double(dependency.to_s).tap do |double|
    modify_dependencies do |dependencies|
      dependencies.service(dependency) do |config|
        double
      end
    end
  end
end