zlost
How to use jason @derive in test?
code
defmodule LibJasonTest do
use ExUnit.Case
defmodule Category do
@type t :: %__MODULE__{
id: Integer.t(),
name: String.t()
}
@derive Jason.Encoder
defstruct [:id, :name]
end
defmodule Book do
@type t :: %__MODULE__{
id: Integer.t(),
name: String.t(),
price: Float.t(),
categories: [Category.t()],
created_at: DateTime.t()
}
@derive Jason.Encoder
defstruct [
:id,
:name,
:price,
:categories,
:created_at
]
end
test "encode / decode" do
book = %Book{}
dbg(Jason.encode!(book))
end
end
error info
1) test encode / decode (LibJasonTest)
test/lib_jason_test.exs:33
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented
for %LibJasonTest.Book{id: nil, name: nil, price: nil, categories: nil,
created_at: nil} of type LibJasonTest.Book (a struct), Jason.Encoder pr
otocol must always be explicitly implemented.
If you own the struct, you can derive the implementation specifying
which fields should be encoded to JSON:
@derive {Jason.Encoder, only: [....]}
defstruct ...
It is also possible to encode all fields, although this should be u
sed carefully to avoid accidentally leaking private information when new
fields are added:
@derive Jason.Encoder
defstruct ...
Finally, if you don't own the struct you want to encode to JSON, yo
u may use Protocol.derive/3 placed outside of any module:
Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])
Protocol.derive(Jason.Encoder, NameOfTheStruct)
. This protocol is implemented for the following type(s): Any, Atom
, BitString, Date, DateTime, Decimal, Float, Integer, Jason.Fragment, Ja
son.OrderedObject, List, Map, NaiveDateTime, Time
code: dbg(Jason.encode!(book))
stacktrace:
(jason 1.4.1) lib/jason.ex:164: Jason.encode!/2
test/lib_jason_test.exs:36: (test)
Finished in 0.1 seconds (0.00s async, 0.1s sync)
1 test, 1 failure
Marked As Solved
zachallaun
As an optimization, protocols in Elixir go through a compile-time process called consolidation, which has the “drawback” of making it impossible to add new protocol implementations at runtime. Because tests are dynamically evaluated, they live in that “runtime” category.
You can read about consolidation here. It has a note about implementing protocols in tests and offers two possible fixes, one of which is to just disable protocol consolidation in tests.
2
Popular in Questions
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







