Giter Site home page Giter Site logo

Comments (6)

philipgiuliani avatar philipgiuliani commented on May 6, 2024

I also already wondered if there is a way to do this. Especially useful when using the Flexible Factory stuff.

But it looks like this gem doesn't either...

from ex_machina.

paulcsmith avatar paulcsmith commented on May 6, 2024

By default we use a single factory file for all factory in factory_girl, but I see your point.

If you want different modules/files per factory then I would create them in test/support/#{factory_name}_factory.ex then import/alias those in your ConnCase/ModelCase or on a per test basis. Does that answer your question?

There are some thoughts on this here: https://github.com/thoughtbot/ex_machina#where-to-put-your-factories but maybe it could be improved. Any ideas?

from ex_machina.

paulcsmith avatar paulcsmith commented on May 6, 2024

@philipgiuliani Also, the flexible factory stuff is much easier in ExMachina 1.0 beta (on master) because you can put the functions right below the factory that needs them

def user_factory do
  %User{}
end

def make_admin(user) do
  %{user | admin: true}
end

That's one of the main reasons the factory definition syntax was changed for 1.0 :)

from ex_machina.

philipgiuliani avatar philipgiuliani commented on May 6, 2024

Oh thanks, yes im using 1.0-beta and i really like it. Actually i haven't thought about splitting them up and importing them into the App.Factory module.

This could be a good Solution to split it up. Maybe we could add this "idea" to the README.

defmodule MyApp.Factory do
  use ExMachina.Ecto, repo: MyApp.Repo

  import MyApp.Factory.User
  import MyApp.Factory.Article
  ...
end

defmodule MyApp.Factory.User do
  def user_factory do
    %User{}
  end

  def make_admin(user) do
    %{user | admin: true}
  end
end

from ex_machina.

paulcsmith avatar paulcsmith commented on May 6, 2024

That won't work since the import will only be availably inside of factory, but you could do

defmodule MyApp.Factory do
  use ExMachina.Ecto, repo: MyApp.Repo

  # Call `use MyApp.Factory`
  defmacro __using__(_opts) do
    quote do
      import MyApp.Factory.User
      import MyApp.Factory.Article
    end
  end
  ...
end

defmodule MyApp.Factory.User do
  def user_factory do
    %User{}
  end

  def make_admin(user) do
    %{user | admin: true}
  end
end

I will consider adding to the README. I'll have to try it out and hear what people think first. I'd rather not suggest something on the README before knowing it's a good idea :P

from ex_machina.

philipgiuliani avatar philipgiuliani commented on May 6, 2024

Oh exactly, thanks. My project is not big enough yet to use it.. Maybe @asiniy can try it and give feedback!

from ex_machina.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.