---
title: ExMachina 2.0 - Cast Away
teaser: ExMachina 2.0 brings new features and functions for easier testing.
tags: elixir,testing,web
author: Josh Steiner
published_on: 2017-03-13
---

[ExMachina](https://github.com/thoughtbot/ex_machina) is a library for creating
test data in Elixir. We've made a number of improvements and bug fixes since
[1.0](https://thoughtbot.com/blog/announcing-exmachina-10--factories-with-a-functional-twist).
Read on for notable changes, or checkout the [CHANGELOG] for a full list of
changes.

## Automatic Casting

The biggest change in ExMachina 2.0 is the automatic casting of values on
`insert`. Now, instead of having to cast dates or decimals manually, you can let
ExMachina do the dirty work for you. Consider this schema:

```elixir
schema "items" do
  field :price, :decimal
end
```

In ExMachina 1.0, you'd have to set the price as a `Decimal` manually:

```elixir
insert(:item, price: Decimal.new(100))
# => %Item{price: #Decimal<100>}
```

In ExMachina 2.0, you can pass the uncast value as you would in Ecto, and
ExMachina will handle casting for you:

```elixir
insert(:item, price: 100)
# => %Item{price: #Decimal<100>}
```

## `params_*` improvements

Thanks to [pdawczak](https://github.com/pdawczak) for contributing a new
function `string_params_for`, which is handy for generating params in controller
test. We've also made [some][] [changes][] to the `params` family of functions, to
better handle `nil` values and associations.

[some]: https://github.com/thoughtbot/ex_machina/pull/148
[changes]: https://github.com/thoughtbot/ex_machina/pull/174

## Upgrading to ExMachina 2.0

Due to changes with the `params` functions, there may be some breakage in
upgrading. See the [CHANGELOG] for more details about what’s changed.

[CHANGELOG]: https://github.com/thoughtbot/ex_machina/blob/master/CHANGELOG.md#200

## Thanks to our contributors

As always, thanks to all of our
[contributors](https://github.com/thoughtbot/ex_machina/graphs/contributors),
who make open source software possible!
