加速测试套件,并实现易于维护的测试用例。替换缓慢的工厂,避免维护固定配置的负担。快速测试,清晰的数据集。
Oaken is fixtures + factories + seeds for your Rails development & test environments.
You may want to head straight to the examples directory.
In development:
db/seeds.rb that feel like Rails.In tests, reuse development seed data:
Fast tests! Seed shared records once and reuse them across tests, transactions rollback changes.
Visibility into your system: factories optimize for isolation, which make it easy to get started, but becomes more complex over time.
Save 20 lines of setup per test case.
You can name things: don't lose hours debugging factory-based tests because everything's anonymous, like I have.
Less mental load: factories' structure requires you to put things together in your head. How many models and associations does one factory create?
DRY setup:
Also worth noting that you
consoleOaken takes inspiration from Rails' fixtures' approach of storytelling about your app's object graph, but replaces the nightmare YAML-based UX with Ruby-based data scripts. This makes data much easier to reason about & connect the dots.
In Oaken, you start by creating a root-level model, typically an Account, Team, or Organization etc. to group everything on, in a scenario. From your root-level model, you then start building your object graph by mirroring how your app works.
So what comes next in your account flow? Maybe it's creating Users on the account.
You can go further if you need to. Is the Account about selling something, like donuts? Maybe you add a menu and some items.
It'll look like this:
account = accounts.create :kaspers_donuts, name: "Kasper's Donuts"
kasper = users.create :kasper, name: "Kasper", email_address: "[email protected]", accounts: [account]
coworker = users.create :coworker, name: "Coworker", email_address: "[email protected]", accounts: [account]
menu = menus.create(account:)
plain_donut = menu_items.create menu:, name: "Plain", price_cents: 10_00
sprinkled_donut = menu_items.create menu:, name: "Sprinkled", price_cents: 10_10[!NOTE]
createtakes an optional symbol label. This makes the record accessible in tests, e.g.users.create :kasperlets tests dosetup { @kasper = users.kasper }.
With fixtures, this would be 4 different files in test/fixtures for accounts, users, menus, and menu_items. It would be ~20 lines of YAML versus ~6 lines of Ruby for this data.
Another issue in fixture files, is that objects from different scenarios are all mixed together making it hard to get a picture of what's going on — even in small apps.
Fixtures also require you to label every record and make them unique throughout your dataset — you have to be careful not to create clashes. This gets difficult to manage quickly and requires diligence on a team that's trying to ship.
However, often the fact that a record is associated onto another is enough. So in Oaken, we let you skip naming every record. Notice how the menus.create & menu_items.create calls don't pass symbol labels. You can still get at them in tests though if you really need to with accounts.kaspers_donuts.menus.first.menu_items.first.
See the fixtures version
# test/fixtures/accounts.yml
kaspers_donuts:
name: Kasper's Donuts
# test/fixtures/users.yml
kasper:
name: "Kasper"
email_address: "[email protected]"
accounts: kaspers_donuts
coworker:
name: "Coworker"
email_address: "[email protected]"
accounts: kaspers_donuts
# test/fixtures/menus.yml
basic:
account: kaspers_donuts
# test/fixtures/menu/items.yml
plain_donut:
menu: basic
name: Plain
price_cents: 10_00
sprinkled_donut:
menu: basic
name: Sprinkled
price_cents: 10_10The reason you go through all the trouble of massaging your fixture files is to have a stable named dataset across test runs that's relatively quick to load — so the database call cost is amortized across tests.
Oaken mirrors this approach giving you stability in your dataset and the relative quickness to insert the data.
For instance, if you have 10 tests that each need the same 2 records, Oaken puts them in the database once before tests run, same as fixtures.
The tradeoff is that if you run just 1 test we'll still seed those 2 same records, but we'll also seed any other record you've added to the shared dataset that might not be needed in those tests.
We rely on Rails' tests being wrapped in transactions so any changes are rolled back after the test case run.
[!NOTE] It can be a good idea to structure your object graph so you won't need database records for your tests — reality can sometimes be far from that ideal state though. Oaken aims to make your present reality easier and something you can improve.
Factories can let you start an app easier. It's just this one factory for now, ok, easy enough.
Over time, however, many teams find their factory based test suite slows to a crawl. Suddenly one factory ends up pulling in the rest of the app.
Factories end up requiring a lot of diligence and passing just the right things in just-so to make managable.
Oaken does away with this. See the sections on the fixtures comparisons above for how.
[!WARNING] Full Disclaimer: while I have worked on systems using factories, I overall don't get it and the fixtures approach makes more sense to me despite the UX issues. I'm trying to support a partial factories approach here in Oaken, see the below section for that, and I'm open to ideas here.
[!TIP] Oaken is compatible with FactoryBot and Fabrication, and they should be able to work together. I consider it a bug if there's compatibility issues, so please open an issue here if you find something.
See the sections on defaults & helpers below.
The aim for Oaken is to have most of the feature set of factories for a fraction of the implementation complexity.
Oaken upgrades seeds in db/seeds.rb, so you can put together scenarios & reuse the development data in tests.
This way, the data you see in your browser, is the same data you work with in tests to make your object graph easier to get — especially for people new to your codebase.
So you get a cohesive & stable dataset with a story like fixtures & their fast loading. But you also get the dynamics of FactoryBot/Fabrication as well without making tons of one-off records to handle each case.
The end result is you end up writing less data back & forth to the database because you aren’t cobbling stuff together.
But seriously; Oaken is one of the single greatest tools I've added to my belt in the past year
It's made cross-environment shared data, data prepping for demos, edge-case tests, and overall development much more reliable & shareable across a team
Thanks for this wonderful project! My head doesn't grok factories and I just want vanilla Rails testing with something better than fixtures, and this is so much better.
The default YAML fixtures is somehow both too simple and too convoluted. It's hard to reference complex associations with it, and the fact that the data gets spread across many files makes it hard to read and harder to grasp.
Oaken, on the other hand, is closer to using the console, which we already know, only in a repeatable and tidier way. The testing data/seeds setup process just feels more intentional this way.
We're using accounts.create and such instead of Account.create! to help enforce consistency & constrain your Ruby usage. This also allows for extra features like defaults and helpers that take way less to implement.
We don't want to be a costly DSL that takes ages to learn and relearn when you come back to it.
We're aiming for a time-to-understand of less than an hour. Same goes for the internals, if you dive in, it should ideally take less than 1 hour to comprehend most of it.
We share similar sentiments to the Pkl configuration language. You may find the ideas helpful before using Oaken.
Oddly enough Oaken came out before Pkl, I just read the ideas here and went "yes, exactly!"
By default, Oaken.loader returns an Oaken::Loader instance to handle loading seed files.
You can load a seed directory via Oaken.loader.seed. You can also load a file, it'll technically just be a match that happens to only hit one file.
So if you call Oaken.loader.seed :accounts, we'll look within db/seeds/ and db/seeds/#{Rails.env}/ and match accounts{,**/*}.rb. So these files would be found:
[!TIP] You can call
Oaken.loader.globwith a single identifier to see what files we'll match. > Some samples:Oaken.loader.glob :accounts,Oaken.loader.glob "cases/pagination".
[!TIP] Putting a file in the top-level
db/seedsversusdb/seeds/developmentordb/seeds/testmeans it's shared in both environments. See below for more tips.
Any directories and/or single-file matches are loaded in the order they're specified. So loader.seed :data, :accounts would first load data and then accounts.
[!IMPORTANT] Understanding and making effective use of Oaken's directory loading will pay dividends for your usage. You generally want to have 1 top-level directive
seedcall to dictate how seeding happens in e.g.db/seeds.rband then let individual seed files load in no specified order within that.
setup phaseWhen you call Oaken.seed/Oaken.loader.seed we'll also call seed :setup fo
暂无开放 Issues,或尚未同步最近议题。