几天前,我看到了 José Valim Mocks 的一篇文章 和明确的合同,并决定将这些模式实施到我自己的项目wikix中。
这些是我的 config/*.exs文件。如您所见(在 test.exs 和 prod.exs 中),我定义了几个环境变量“http_client”和“user_agent”。这样,当我测试模块时,我使用模拟,当模块投入生产时,我使用真正的 http 客户端。
问题是:当我尝试在我的 phoenix 应用程序中使用 wikix 作为依赖项时,由于某种原因,mix 忽略了我的 wikix/config/prod.exs 文件,因此从未定义过 http 客户端(在 wikix 中)。
在我的凤凰应用程序中,我得到了 nil.HTTPClient,而不是 Wikix.HTTPClient。
这是我的 phoenix/mix.exs 文件
defmodule GraapyApi.Mixfile do
use Mix.Project
def project do
[app: :graapy_api,
version: "0.0.1",
elixir: "~> 1.0",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases, deps: deps
]
end
# Configuration for the OTP application.
#
# Type `mix help compile.app` for more information.
def application do
[mod: {GraapyApi, []},
applications: [:phoenix, :cowboy, :logger, :gettext,
:phoenix_ecto, :mongodb_ecto, :wikix]]
end
# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_), do: ["lib", "web"]
# Specifies your project dependencies.
#
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, "~> 1.1.4"},
{:mongodb_ecto, ">= 0.0.0"},
{:phoenix_ecto, "~> 2.0"},
{:gettext, "~> 0.9"},
{:cowboy, "~> 1.0"},
{:dogma, "~> 0.1", only: :dev},
{:credo, "~> 0.3", only: [:dev, :test]},
{:wikix, github: "graapy/wikix"}
]
end
# Aliases are shortcut or tasks specific to the current project.
# For example, to create, migrate and run the seeds file at once:
#
# $ mix ecto.setup
#
# See the documentation for `Mix` for more info on aliases.
defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"]