-
Notifications
You must be signed in to change notification settings - Fork 461
Testing with VCR
In our CI environment, we don't want to run the OBS backend and therefore we are using VCR to replay previous recorded HTTP sessions between frontend and backend.
- In the test environment HTTP requests to the OBS backend are by default disabled.
You can change this by setting the environment variable
GLOBAL_WRITE_THROUGH=truebefore running your spec. - In CI HTTP requests to the backend are by default enabled
- VCR is configured to only record a cassette once.
- VCR mocks responses to HTTP requests by comparing the requested
request.uriwith the URL from cassettes. That means if you have random parts (like project/package names) in the URL requested the cassette will not contain a match and VCR will refuse to change any existing cassette. - VCR can be enabled/disabled per example with
RSpec.describe Package, vcr: false do ... end - VCR cassettes are NEVER edited manually please
We have a configuration global named global_write_through which controls if data will be sent to backend or not. When set to true, data goes to the backend (therefore a backend is expected to be there up and running). This configuration is controlled in vcr.rb.
With vcr: true set in the spec Class description, simply run the spec as normal:
rspec spec/models/workflow_spec.rb
Make sure vcr: false is set in the spec Class description, then run the spec with the GLOBAL_WRITE_THROUGH prefix to override the one described in vcr.rb:
GLOBAL_WRITE_THROUGH=true rspec spec/models/workflow_spec.rb
Set default_cassette_options to :new_episodes instead of :once, then enable vcr: true in the spec file and run:
GLOBAL_WRITE_THROUGH=true rspec spec/models/workflow_spec.rb
With GLOBAL_WRITE_THROUGH enabled it will use the backend, but with vcr: true and :new_episodes it will record everything run by the spec to cassettes.
Same as #New cassette, but this time it will add the new episode recorded to the existing cassette instead.
In the process, of developing tests with cassettes, the backend doesn't get flushed between test runs. If cassettes are recorded without flushing the backend, they will contain responses from a backend filled up with previous data.
So, for example, when in your test you use a factory to create a package package1 with a file and run the test, a package with a file is created in the backend. If you modify the test to create a simple package package1 without a file, and run the test, the backend will keep the file attached to this package. And the rest of the code of your test will work as it was the first run, when it probably shouldn't.
To assure this doesn't happen when creating the cassettes, just delete the docker backend container, and wake up it again. This can be done with the following commands:
docker-compose rm -s -f backend
docker-compose up -d backend
- Development Environment Overview
- Development Environment Tips & Tricks
- Spec-Tips
- Code Style
- Rubocop
- Testing with VCR
- Test in kanku
- Authentication
- Authorization
- Autocomplete
- BS Requests
- Events
- ProjectLog
- Notifications
- Feature Toggles
- Build Results
- Attrib classes
- Flags
- The BackendPackage Cache
- Maintenance classes
- Cloud uploader
- Delayed Jobs
- Staging Workflow
- StatusHistory
- OBS API
- Owner Search
- Search
- Links
- Distributions
- Repository
- Data Migrations
- Package Versions
- next_rails
- Ruby Update
- Rails Profiling
- Remote Pairing Setup Guide
- Factory Dashboard
- osc
- Setup an OBS Development Environment on macOS
- Run OpenQA smoketest locally
- Responsive Guidelines
- Importing database dumps
- Problem Statement & Solution
- Kickoff New Stuff
- New Swagger API doc
- Documentation and Communication
- GitHub Actions
- Brakeman
- How to Introduce Software Design Patterns
- Query Objects
- Services
- View Components
- RFC: Core Components
- RFC: Decorator Pattern
- RFC: Backend models
- RFC: Hotwire Turbo Frames Pattern