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
- #build_container ⇒ Object private
-
#dependencies ⇒ Object
The current Container which will be injected into the test Rack session.
-
#modify_dependencies {|Container| ... } ⇒ Object
Convenience for injecting a modified container into the test rack session.
-
#setup_controller_request_and_response ⇒ Object
private
Builds an empty Container in the fake Rack environment.
-
#stub_factory(dependency) ⇒ RSpec::Mocks::TestDouble
Injects a stubbed factory into the test Container and returns it.
-
#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.
-
#stub_service(dependency) ⇒ RSpec::Mocks::TestDouble
Injects a stubbed service into the test Container and returns it.
Instance Method Details
#build_container ⇒ Object
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 |
#dependencies ⇒ Object
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.
69 70 71 |
# File 'lib/payload/testing.rb', line 69 def modify_dependencies @request.env[:dependencies] = yield(dependencies) end |
#setup_controller_request_and_response ⇒ Object
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.
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.
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.
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 |