Elixir **(CompileError)test/integration/listing_authors.test.exs:9:BookcaseApi.Author._struct__/1
我有一个集成测试,我正在工作,但现在它不会编译 列出作者.test.exsElixir **(CompileError)test/integration/listing_authors.test.exs:9:BookcaseApi.Author._struct__/1,elixir,phoenix-framework,Elixir,Phoenix Framework,我有一个集成测试,我正在工作,但现在它不会编译 列出作者.test.exs defmodule ListingAuthorsIntegrationTest do use ExUnit.Case, async: true use Plug.Test alias BookcaseApi.Web.Router alias BookcaseApi.Author @opts Router.init([]) test 'listing authors' do author
defmodule ListingAuthorsIntegrationTest do
use ExUnit.Case, async: true
use Plug.Test
alias BookcaseApi.Web.Router
alias BookcaseApi.Author
@opts Router.init([])
test 'listing authors' do
author = %Author{name: "Oscar Wilde"}
|> Repo.insert!
# Note: single quotes are for char lists.
# Note: double quotes are for strings.
conn = conn(:get, "/authors")
response = Router.call(conn, @opts)
assert response.status == 200
assert response.resp_body == author
end
end
author.ex
defmodule BookcaseApi.Author do
use BookcaseApi.Web, :model
schema "name" do
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
stacktrace.txt
λ mix test test/integration/listing_authors.test.exs 0 < 18:39:12
** (CompileError) test/integration/listing_authors.test.exs:9: BookcaseApi.Author.__struct__/1 is undefined, cannot expand struct BookcaseApi.Author
(stdlib) lists.erl:1354: :lists.mapfoldl/3
test/integration/listing_authors.test.exs:8: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
然后我用下面的命令为项目生成了一个模型
mix phx.new bookcase_api --no-html --no-brunch
mix phoenix.gen.json Author name
我猜您的问题是在phoenix 1.3项目中使用
mix phoenix.gen.json
(这是phoenix 1.2生成器)。尝试改用mix phx.gen.json
。您的模型试图使用BookcaseApi.Web,:model
,而Phoenix 1.3 schemes应该使用exto.Schema
。Dennis已经指出,您对应用程序和模型使用了两种不同的生成器,因此这肯定是您问题的根源。生成的模型也存在另一个问题。看起来您打算用一个名称
字段生成一个作者
架构,但看起来您没有给它传递数据库表的名称,因此它将名称
解释为表名。对于新的生成器,您还需要为它提供一个上下文——在本例中,您可能希望使用Authors,以防您希望添加更多与Authors api相关的模块。因此,生成器命令将是:
mix phx.gen.json作者姓名:string
(:string在技术上可以省略,除非另有说明,否则生成器假定您希望字段为:string time)
正确生成的结构如下所示:
defmodule BookcaseApi.Authors.Author do
use Ecto.Schema
schema "authors" do
field :name, :string
timestamps
end
end
菲尼克斯网站的文档尚未更新,以涵盖新的发电机,但源文件有很好的文档记录。以下是新json生成器的文档(也可以通过
mix help phx.gen.json
访问):这很奇怪。。这在iex-S mix中有效吗?:%BookcaseApi.Author{name:“Oscar Wilde”}
?如果是,您可以尝试执行一次mixclean&&mixcompile
,然后运行测试吗?@Dogbert感谢您的评论,但是iex-smix
:%BookcaseApi。作者{name:“Oscar Wilde”}
不起作用,并显示与上述相同的错误消息。