47
Making a Mock Kelsey Shannahan

Making a Mock by Kelsey Shannahan

  • Upload
    qaoth

  • View
    49

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Making a Mock by Kelsey Shannahan

Making a MockKelsey Shannahan

Page 2: Making a Mock by Kelsey Shannahan

Making a Mock

•Reasons to use a mock

•How we built a mock

•Use cases and limitations of a mock

Page 3: Making a Mock by Kelsey Shannahan

Why Use a Mock?

•This leads us to a more important question…

•What is a mock?

Page 4: Making a Mock by Kelsey Shannahan

What is a mock?•A mock is stand-in for another system that you either do not have control over or do not want to setup during the course of testing.

Page 5: Making a Mock by Kelsey Shannahan

Why use a mock?•Mocks allow us to test a part of the system in isolation from outside factors.•Mocks can also replace applications we cannot run in testing• Internal applications owned by other teams• Applications that cannot be setup in testing• Systems that are completely out of your control (third-party)•Gives us control over the app so we can modify how it interacts with us

Page 6: Making a Mock by Kelsey Shannahan

Should something be mocked?

•Does it fulfill these criteria?•Required by the system under test

BUT•Is not actually under test

Page 7: Making a Mock by Kelsey Shannahan

A critical part of our process is out of our control

System A sends a message to System B

System B goes and does things with message

System B returns amessage to System A

Page 8: Making a Mock by Kelsey Shannahan

How do we test end-to-end?

•Rely on unit tests?

•Reach out to the partner every time you need to test?• How do you deploy quickly then?• What if your change is agnostic to your partner’s process?

•Drop it into production and hope for the best?

Page 9: Making a Mock by Kelsey Shannahan
Page 10: Making a Mock by Kelsey Shannahan

How do we test end-to-end?

•Rely on unit tests?

•Reach out to the partner every time you need to test?• How do you deploy quickly then?• What if your change is agnostic to your partner’s process?

•Drop it into production and hope for the best?

•Hook it up to their QA endpoint?

Page 11: Making a Mock by Kelsey Shannahan

The partner isn’t expecting a message

Page 12: Making a Mock by Kelsey Shannahan

We want to load test

Page 13: Making a Mock by Kelsey Shannahan

Build a stand-in system

Page 14: Making a Mock by Kelsey Shannahan

Build a stand-in system

Page 15: Making a Mock by Kelsey Shannahan

Build a stand-in system

Page 16: Making a Mock by Kelsey Shannahan

Build a stand-in system

Page 17: Making a Mock by Kelsey Shannahan

Why use a mock• Allows us to test our code in isolation• Replaces a system we cannot use in testing• Gives us control over the app so we can adjust to

our needs

Page 18: Making a Mock by Kelsey Shannahan

How we built our mock

Page 19: Making a Mock by Kelsey Shannahan

How do we build mocks?• Ruby has a number of libraries that help mock out

code for unit tests• Rspec (okay there’s other stuff but who cares)

Page 20: Making a Mock by Kelsey Shannahan

If you want to know more about non-end to end mocks…

Go to Bradley Temple’s “Testing Microservices”!

This was not planned it just sort of happened.

Page 21: Making a Mock by Kelsey Shannahan

End To End Testing

• What the mock looks like will vary based on what you need it to do.• Speaking generally, you can build an shell

application to stand-in for your mocked service.

• This requires a bit of technical and a lot of business knowledge.• Technical to create the mock and point other apps at it• Business to know what is being sent, what is expected in

response, what those should look like, and any ”invisible” actions happening inside the mocked process

Page 22: Making a Mock by Kelsey Shannahan

Our Requirements

•Synchronous and asynchronous communication•Message validation using .xsd and internal CMM gems•Ability to modify what responses get sent back•Support for HTTP, SFTP, and SOAP•Compatible with our internal credential security•Ability to add features as needed• Logging• Feature flipping• Error conditions

Page 23: Making a Mock by Kelsey Shannahan

Tools and Tech

• While building it, ask yourself…• Do I want to sink a lot of time into maintaining this?•Ruby on Rails

• because we were already using it•Sidekiq

• because we were already using it•Capistrano

• because we were already using it

Page 24: Making a Mock by Kelsey Shannahan

Now my entire team uses and maintains it!

Page 25: Making a Mock by Kelsey Shannahan

•Created a Rails project

•Yes, it includes specs•Left out what wasn’t necessary• No front-end• No database

Page 26: Making a Mock by Kelsey Shannahan

•The controller does only a few things• Verifies the request from the app is

formed correctly• Returns a status• Queues up a job to later return the

response

Page 27: Making a Mock by Kelsey Shannahan

•The controller does only a few things• Verifies the request from the app is formed

correctly• Returns a status• Queues up a job to later return the response

Page 28: Making a Mock by Kelsey Shannahan

•What was with the plan_processing_time method?•It just delays for a few seconds to mimic actual work being done on the request. •It is also how we can mimic scenarios like slow response times

Page 29: Making a Mock by Kelsey Shannahan

•Helper methods within the control are there to only process the request and extract data we need•No other handling of the request is done – we simply do not care

Page 30: Making a Mock by Kelsey Shannahan

•The worker job handles authenticating back to our app•It accepts a block of xml which it posts to our app

Page 31: Making a Mock by Kelsey Shannahan

•The worker job handles authenticating back to our app•It accepts a block of xml which it posts to our app

Page 32: Making a Mock by Kelsey Shannahan

•The response to the app is stored as xml•We swap in whatever data we need and use a generic template for everything else

Page 33: Making a Mock by Kelsey Shannahan

•That’s all that we do to send a response!

Page 34: Making a Mock by Kelsey Shannahan

How we found our templates• Some of our templates were defined by business

requirements• Others were used by unit tests• For the rest, we inspected logs

Page 35: Making a Mock by Kelsey Shannahan

How to build a mock

• Keep it simple• Use tools that are either easy or well-understood• Only build what you need

Page 36: Making a Mock by Kelsey Shannahan

Uses and Limitations

Page 37: Making a Mock by Kelsey Shannahan

Mocks don’t solve everything

•Mocks do not replicate the behavior of the mocked system• This is especially true when the system is third-party and you

don’t know exactly what it does

•The system being mocked may not do what you expect, or it may change•In short: this isn’t a replacement for integration testing

Page 38: Making a Mock by Kelsey Shannahan

They do give you a lot of flexibility

•Here’s how we’ve used it…

•Obviously, we use it for end-to-end testing.•Our mock can now replicate responses for every integration that is outside of our control.

•We can now hand off an almost finished change for integration testing• Fix bugs early, without using up your partner’s testing time!

Page 39: Making a Mock by Kelsey Shannahan

Use Cases•We had a scenario where we wanted to test how our system handles bad responses from the other application•Tweaked the endpoint to return 500 responses instead of a message•Deployed that branch of code to our test server

Page 40: Making a Mock by Kelsey Shannahan

Use Cases

•Debugging.

Page 41: Making a Mock by Kelsey Shannahan

Use Cases•Load testing or stress testing the system

•The advantage of a mock is that it does almost no processing behind the scenes

•This allows it to keep up with load that may reveal weaknesses in the system under test

Page 42: Making a Mock by Kelsey Shannahan

Preserving Functionality

•Flipper•Data based decisions

Page 43: Making a Mock by Kelsey Shannahan

Flipper

Page 44: Making a Mock by Kelsey Shannahan

Data based decisions

•Our mock only has the information we give it from the calling app•We use this to decide what sort of response to send

Page 45: Making a Mock by Kelsey Shannahan

Uses and Limitations

•Cannot fully replace integration testing•Allows for early testing during development•Can be used to create specialized scenarios

Page 46: Making a Mock by Kelsey Shannahan

Making a Mock

•Reasons to use a mock

•How we built a mock

•Use cases and limitations of a mock

Page 47: Making a Mock by Kelsey Shannahan