How to reference outside Contexts in test files?
I'm really struggling to understand Contexts in Phoenix Elixir. I have three contexts, Auth
(contains User.ex
), Groups
(Circle.ex
), and Content
(ShareMark.ex
). Within each of those, there are schemas, users
, circles
, and sharemarks
respectively.
I'm trying to figure out how to use the pre-provided create_circle
outside of the Groups
context. Is there something a context is analogous to in Ruby?
Within content_test.ex
, I am trying to define the following
@valid_attrs %circle: Groups.create_circle(%name: "My test"), url: "google.com", title: "Google"
defmodule ShareMark.ContentTest do
use ShareMark.DataCase
alias ShareMark.Content
use ShareMark.Groups
describe "sharemarks" do
alias ShareMark.Content.ShareMark
@valid_attrs %circle: Groups.create_circle(%name: "Evan's test"), url: "google.com", title: "Google"
@update_attrs %circle: Groups.create_circle(%name: "Mike's test"), url: "duckduckgo.com", title: "DuckDuckGo"
@invalid_attrs %circle: Groups.create_circle(%name: "Bad test")
def sharemark_fixture(attrs \ %) do
:ok, sharemark =
attrs
|> Enum.into(@valid_attrs)
|> Content.create_sharemark()
sharemark
end
...
end
Here is circle.ex
defmodule ShareMark.Groups.Circle do
use Ecto.Schema
import Ecto.Changeset
schema "circles" do
field :name, :string
field :creator_id, :id
many_to_many :members, ShareMark.Auth.User, join_through: "users_circles"
has_many :sharemarks, ShareMark.Content.ShareMark
timestamps()
end
@doc false
def changeset(circle, attrs) do
circle
|> cast(attrs, [:name])
|> validate_required([:name])
end
end
But it is giving the following error:
** (CompileError) test/sharemark/content/content_test.exs:8: undefined function create_circle/1
Google is completely unhelpful, as Phoenix has so few questions asked about it. Sorry for such a novice question.
elixir phoenix-framework
add a comment |
I'm really struggling to understand Contexts in Phoenix Elixir. I have three contexts, Auth
(contains User.ex
), Groups
(Circle.ex
), and Content
(ShareMark.ex
). Within each of those, there are schemas, users
, circles
, and sharemarks
respectively.
I'm trying to figure out how to use the pre-provided create_circle
outside of the Groups
context. Is there something a context is analogous to in Ruby?
Within content_test.ex
, I am trying to define the following
@valid_attrs %circle: Groups.create_circle(%name: "My test"), url: "google.com", title: "Google"
defmodule ShareMark.ContentTest do
use ShareMark.DataCase
alias ShareMark.Content
use ShareMark.Groups
describe "sharemarks" do
alias ShareMark.Content.ShareMark
@valid_attrs %circle: Groups.create_circle(%name: "Evan's test"), url: "google.com", title: "Google"
@update_attrs %circle: Groups.create_circle(%name: "Mike's test"), url: "duckduckgo.com", title: "DuckDuckGo"
@invalid_attrs %circle: Groups.create_circle(%name: "Bad test")
def sharemark_fixture(attrs \ %) do
:ok, sharemark =
attrs
|> Enum.into(@valid_attrs)
|> Content.create_sharemark()
sharemark
end
...
end
Here is circle.ex
defmodule ShareMark.Groups.Circle do
use Ecto.Schema
import Ecto.Changeset
schema "circles" do
field :name, :string
field :creator_id, :id
many_to_many :members, ShareMark.Auth.User, join_through: "users_circles"
has_many :sharemarks, ShareMark.Content.ShareMark
timestamps()
end
@doc false
def changeset(circle, attrs) do
circle
|> cast(attrs, [:name])
|> validate_required([:name])
end
end
But it is giving the following error:
** (CompileError) test/sharemark/content/content_test.exs:8: undefined function create_circle/1
Google is completely unhelpful, as Phoenix has so few questions asked about it. Sorry for such a novice question.
elixir phoenix-framework
Does theGroups.create_circle
method by any chance interact with the database?
– Sheharyar
Nov 13 '18 at 9:50
It does it does. It creates a circle and adds it to the DB.|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12
add a comment |
I'm really struggling to understand Contexts in Phoenix Elixir. I have three contexts, Auth
(contains User.ex
), Groups
(Circle.ex
), and Content
(ShareMark.ex
). Within each of those, there are schemas, users
, circles
, and sharemarks
respectively.
I'm trying to figure out how to use the pre-provided create_circle
outside of the Groups
context. Is there something a context is analogous to in Ruby?
Within content_test.ex
, I am trying to define the following
@valid_attrs %circle: Groups.create_circle(%name: "My test"), url: "google.com", title: "Google"
defmodule ShareMark.ContentTest do
use ShareMark.DataCase
alias ShareMark.Content
use ShareMark.Groups
describe "sharemarks" do
alias ShareMark.Content.ShareMark
@valid_attrs %circle: Groups.create_circle(%name: "Evan's test"), url: "google.com", title: "Google"
@update_attrs %circle: Groups.create_circle(%name: "Mike's test"), url: "duckduckgo.com", title: "DuckDuckGo"
@invalid_attrs %circle: Groups.create_circle(%name: "Bad test")
def sharemark_fixture(attrs \ %) do
:ok, sharemark =
attrs
|> Enum.into(@valid_attrs)
|> Content.create_sharemark()
sharemark
end
...
end
Here is circle.ex
defmodule ShareMark.Groups.Circle do
use Ecto.Schema
import Ecto.Changeset
schema "circles" do
field :name, :string
field :creator_id, :id
many_to_many :members, ShareMark.Auth.User, join_through: "users_circles"
has_many :sharemarks, ShareMark.Content.ShareMark
timestamps()
end
@doc false
def changeset(circle, attrs) do
circle
|> cast(attrs, [:name])
|> validate_required([:name])
end
end
But it is giving the following error:
** (CompileError) test/sharemark/content/content_test.exs:8: undefined function create_circle/1
Google is completely unhelpful, as Phoenix has so few questions asked about it. Sorry for such a novice question.
elixir phoenix-framework
I'm really struggling to understand Contexts in Phoenix Elixir. I have three contexts, Auth
(contains User.ex
), Groups
(Circle.ex
), and Content
(ShareMark.ex
). Within each of those, there are schemas, users
, circles
, and sharemarks
respectively.
I'm trying to figure out how to use the pre-provided create_circle
outside of the Groups
context. Is there something a context is analogous to in Ruby?
Within content_test.ex
, I am trying to define the following
@valid_attrs %circle: Groups.create_circle(%name: "My test"), url: "google.com", title: "Google"
defmodule ShareMark.ContentTest do
use ShareMark.DataCase
alias ShareMark.Content
use ShareMark.Groups
describe "sharemarks" do
alias ShareMark.Content.ShareMark
@valid_attrs %circle: Groups.create_circle(%name: "Evan's test"), url: "google.com", title: "Google"
@update_attrs %circle: Groups.create_circle(%name: "Mike's test"), url: "duckduckgo.com", title: "DuckDuckGo"
@invalid_attrs %circle: Groups.create_circle(%name: "Bad test")
def sharemark_fixture(attrs \ %) do
:ok, sharemark =
attrs
|> Enum.into(@valid_attrs)
|> Content.create_sharemark()
sharemark
end
...
end
Here is circle.ex
defmodule ShareMark.Groups.Circle do
use Ecto.Schema
import Ecto.Changeset
schema "circles" do
field :name, :string
field :creator_id, :id
many_to_many :members, ShareMark.Auth.User, join_through: "users_circles"
has_many :sharemarks, ShareMark.Content.ShareMark
timestamps()
end
@doc false
def changeset(circle, attrs) do
circle
|> cast(attrs, [:name])
|> validate_required([:name])
end
end
But it is giving the following error:
** (CompileError) test/sharemark/content/content_test.exs:8: undefined function create_circle/1
Google is completely unhelpful, as Phoenix has so few questions asked about it. Sorry for such a novice question.
elixir phoenix-framework
elixir phoenix-framework
edited Nov 13 '18 at 21:40
Sheharyar
46.1k12110164
46.1k12110164
asked Nov 13 '18 at 5:29
PianoFingersPianoFingers
375
375
Does theGroups.create_circle
method by any chance interact with the database?
– Sheharyar
Nov 13 '18 at 9:50
It does it does. It creates a circle and adds it to the DB.|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12
add a comment |
Does theGroups.create_circle
method by any chance interact with the database?
– Sheharyar
Nov 13 '18 at 9:50
It does it does. It creates a circle and adds it to the DB.|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12
Does the
Groups.create_circle
method by any chance interact with the database?– Sheharyar
Nov 13 '18 at 9:50
Does the
Groups.create_circle
method by any chance interact with the database?– Sheharyar
Nov 13 '18 at 9:50
It does it does. It creates a circle and adds it to the DB.
|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12
It does it does. It creates a circle and adds it to the DB.
|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12
add a comment |
2 Answers
2
active
oldest
votes
In your test you have this line:
use ShareMark.Groups
this should be an alias
statement:
alias ShareMark.Groups
add a comment |
For starters, phoenix-framework does not force you to use Contexts, they're just a way of better organizing your code. This makes them slightly more confusing for beginners, compared to the File-Type First (FTF) structure of rails applications, but makes the code heirarchy much more easier to understand and manage in the long haul.
You can choose to use contexts or just put all modules together. Either way, whatever public functions you define are acessible from anywhere else in the app (as long as you use the correct module name to call them).
More resources on Contexts:
- Youtube: Chris McCord on Contexts
- Hexdocs: Phoenix Contexts
- Blog Post: Organizing Large React Apps
(Not about Elixir, but still a good overview)
Now on to your actual code, there are two problems with it.
First, as @Paweł mentioned, you need to alias
your module or use the full name:
alias ShareMark.Groups
Second, you're calling Groups.create_circle
in a module attribute (@value
). Module attributes aren't like your regular "variables", they are resolved at compile-time. Meaning, in your case, they will try to write to the database before you even start your test suite.
To fix that, either move your initialization logic to your actual test or into ExUnit's setup/1
callback:
setup do
%circle: Groups.create_circle(%name: "Test Circle"
end
test "something", %circle: circle do
valid_attrs = %circle: circle, url: "google.com", title: "Google"
# assert something
end
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274382%2fhow-to-reference-outside-contexts-in-test-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your test you have this line:
use ShareMark.Groups
this should be an alias
statement:
alias ShareMark.Groups
add a comment |
In your test you have this line:
use ShareMark.Groups
this should be an alias
statement:
alias ShareMark.Groups
add a comment |
In your test you have this line:
use ShareMark.Groups
this should be an alias
statement:
alias ShareMark.Groups
In your test you have this line:
use ShareMark.Groups
this should be an alias
statement:
alias ShareMark.Groups
answered Nov 13 '18 at 7:03
Paweł DawczakPaweł Dawczak
7,56311430
7,56311430
add a comment |
add a comment |
For starters, phoenix-framework does not force you to use Contexts, they're just a way of better organizing your code. This makes them slightly more confusing for beginners, compared to the File-Type First (FTF) structure of rails applications, but makes the code heirarchy much more easier to understand and manage in the long haul.
You can choose to use contexts or just put all modules together. Either way, whatever public functions you define are acessible from anywhere else in the app (as long as you use the correct module name to call them).
More resources on Contexts:
- Youtube: Chris McCord on Contexts
- Hexdocs: Phoenix Contexts
- Blog Post: Organizing Large React Apps
(Not about Elixir, but still a good overview)
Now on to your actual code, there are two problems with it.
First, as @Paweł mentioned, you need to alias
your module or use the full name:
alias ShareMark.Groups
Second, you're calling Groups.create_circle
in a module attribute (@value
). Module attributes aren't like your regular "variables", they are resolved at compile-time. Meaning, in your case, they will try to write to the database before you even start your test suite.
To fix that, either move your initialization logic to your actual test or into ExUnit's setup/1
callback:
setup do
%circle: Groups.create_circle(%name: "Test Circle"
end
test "something", %circle: circle do
valid_attrs = %circle: circle, url: "google.com", title: "Google"
# assert something
end
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
add a comment |
For starters, phoenix-framework does not force you to use Contexts, they're just a way of better organizing your code. This makes them slightly more confusing for beginners, compared to the File-Type First (FTF) structure of rails applications, but makes the code heirarchy much more easier to understand and manage in the long haul.
You can choose to use contexts or just put all modules together. Either way, whatever public functions you define are acessible from anywhere else in the app (as long as you use the correct module name to call them).
More resources on Contexts:
- Youtube: Chris McCord on Contexts
- Hexdocs: Phoenix Contexts
- Blog Post: Organizing Large React Apps
(Not about Elixir, but still a good overview)
Now on to your actual code, there are two problems with it.
First, as @Paweł mentioned, you need to alias
your module or use the full name:
alias ShareMark.Groups
Second, you're calling Groups.create_circle
in a module attribute (@value
). Module attributes aren't like your regular "variables", they are resolved at compile-time. Meaning, in your case, they will try to write to the database before you even start your test suite.
To fix that, either move your initialization logic to your actual test or into ExUnit's setup/1
callback:
setup do
%circle: Groups.create_circle(%name: "Test Circle"
end
test "something", %circle: circle do
valid_attrs = %circle: circle, url: "google.com", title: "Google"
# assert something
end
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
add a comment |
For starters, phoenix-framework does not force you to use Contexts, they're just a way of better organizing your code. This makes them slightly more confusing for beginners, compared to the File-Type First (FTF) structure of rails applications, but makes the code heirarchy much more easier to understand and manage in the long haul.
You can choose to use contexts or just put all modules together. Either way, whatever public functions you define are acessible from anywhere else in the app (as long as you use the correct module name to call them).
More resources on Contexts:
- Youtube: Chris McCord on Contexts
- Hexdocs: Phoenix Contexts
- Blog Post: Organizing Large React Apps
(Not about Elixir, but still a good overview)
Now on to your actual code, there are two problems with it.
First, as @Paweł mentioned, you need to alias
your module or use the full name:
alias ShareMark.Groups
Second, you're calling Groups.create_circle
in a module attribute (@value
). Module attributes aren't like your regular "variables", they are resolved at compile-time. Meaning, in your case, they will try to write to the database before you even start your test suite.
To fix that, either move your initialization logic to your actual test or into ExUnit's setup/1
callback:
setup do
%circle: Groups.create_circle(%name: "Test Circle"
end
test "something", %circle: circle do
valid_attrs = %circle: circle, url: "google.com", title: "Google"
# assert something
end
For starters, phoenix-framework does not force you to use Contexts, they're just a way of better organizing your code. This makes them slightly more confusing for beginners, compared to the File-Type First (FTF) structure of rails applications, but makes the code heirarchy much more easier to understand and manage in the long haul.
You can choose to use contexts or just put all modules together. Either way, whatever public functions you define are acessible from anywhere else in the app (as long as you use the correct module name to call them).
More resources on Contexts:
- Youtube: Chris McCord on Contexts
- Hexdocs: Phoenix Contexts
- Blog Post: Organizing Large React Apps
(Not about Elixir, but still a good overview)
Now on to your actual code, there are two problems with it.
First, as @Paweł mentioned, you need to alias
your module or use the full name:
alias ShareMark.Groups
Second, you're calling Groups.create_circle
in a module attribute (@value
). Module attributes aren't like your regular "variables", they are resolved at compile-time. Meaning, in your case, they will try to write to the database before you even start your test suite.
To fix that, either move your initialization logic to your actual test or into ExUnit's setup/1
callback:
setup do
%circle: Groups.create_circle(%name: "Test Circle"
end
test "something", %circle: circle do
valid_attrs = %circle: circle, url: "google.com", title: "Google"
# assert something
end
edited Nov 13 '18 at 21:55
answered Nov 13 '18 at 21:37
SheharyarSheharyar
46.1k12110164
46.1k12110164
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
add a comment |
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Wonderful. Answers like these are why Stack Overflow is so great. You've given me the answer to my question, and more useful resources so I don't have to keep coming back. Thanks!
– PianoFingers
Nov 14 '18 at 1:37
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
Consider accepting the answer if it solved your problem.
– Sheharyar
Nov 14 '18 at 1:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274382%2fhow-to-reference-outside-contexts-in-test-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does the
Groups.create_circle
method by any chance interact with the database?– Sheharyar
Nov 13 '18 at 9:50
It does it does. It creates a circle and adds it to the DB.
|> Repo.insert()
– PianoFingers
Nov 13 '18 at 15:12