JSFiddle - React, Tailwind, and code Playground
by allisonstrandberg
HTML
<!DOCTYPE html>
<html>
<head>
<title>Testing with Factories</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Testing with factories
---
# Test fixtures
Test fixtures are the default way to set up data for tests in Rails and in Python's pytest.
Rails: https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures
Python: https://docs.pytest.org/en/stable/fixture.html
---
# A Rails fixture
`test/fixtures/filters/queries.yml`
```yaml
one:
sai_min: 0.7
age_min: 30.5
point: "POINT (-87.68 41.90)"
distance: 1000
```
`test/controllers/api/solarsystem/filters_queries_controller_test.rb`
```rails
# ...
setup do
@filters_query = filters_queries(:one)
# ...
end
# And then a test that involves the filters query
```
---
# A pytest test with fixtures
```python
import pytest
class Fruit:
def __init__(self, name):
self.name = name
def __eq__(self, other):
return self.name == other.name
@pytest.fixture
def my_fruit():
return Fruit("apple")
@pytest.fixture
def fruit_basket(my_fruit):
return [Fruit("banana"), my_fruit]
def test_my_fruit_in_basket(my_fruit, fruit_basket):
assert my_fruit in fruit_basket
```
---
# Fluxboard example: testing promotions
```rails
class Promotion < ApplicationRecord
# ...
def free_period
free_days.days
end
end
```
```rails
require 'test_helper'
class PromotionTest < ActiveSupport::TestCase
test 'free_period returns...