How to reference outside Contexts in test files?










0















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.










share|improve this question
























  • 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
















0















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.










share|improve this question
























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 21:40









Sheharyar

46.1k12110164




46.1k12110164










asked Nov 13 '18 at 5:29









PianoFingersPianoFingers

375




375












  • 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


















  • 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

















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













2 Answers
2






active

oldest

votes


















2














In your test you have this line:



use ShareMark.Groups


this should be an alias statement:



alias ShareMark.Groups





share|improve this answer






























    1














    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





    share|improve this answer

























    • 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










    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
    );



    );













    draft saved

    draft discarded


















    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









    2














    In your test you have this line:



    use ShareMark.Groups


    this should be an alias statement:



    alias ShareMark.Groups





    share|improve this answer



























      2














      In your test you have this line:



      use ShareMark.Groups


      this should be an alias statement:



      alias ShareMark.Groups





      share|improve this answer

























        2












        2








        2







        In your test you have this line:



        use ShareMark.Groups


        this should be an alias statement:



        alias ShareMark.Groups





        share|improve this answer













        In your test you have this line:



        use ShareMark.Groups


        this should be an alias statement:



        alias ShareMark.Groups






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 7:03









        Paweł DawczakPaweł Dawczak

        7,56311430




        7,56311430























            1














            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





            share|improve this answer

























            • 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















            1














            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





            share|improve this answer

























            • 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













            1












            1








            1







            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





            share|improve this answer















            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế