ruby - Share traits across different factories -


I have several models that can be authorized (an author's area) and / or tenant (a tenant area) ) Therefore, I have written concerns for both of them.

The problem is in tests. I used the shared_examples_for block to write the tests for the concerns and included them in my model tests. Anyway, to do this, I have many symptoms and after the blocks, for example:

  (create:). Authorize | Authorable.author = build (user, tenant: authorable.tenant) End Symptom: do no_author do (after). Allow Authorable.author = nil end end trait: no_enant do tenant nil end   

This piece of code should be equal in factories of all models that are tenant and authorized.

I could not find any way to do this. Is this possible?

Symptoms can be registered globally, so that they can be used in no other factory Use of the successor of factorial:

  factorygear. Defining property: no_author do after (: build) {| authorable | Authorable.author = nil} end character: no_enant do tenant zero end factory: model tenant {construction (: tenant)} end end   

You can just build your items like this After: factoryGirl.build (: model ,: no_nant) after FactoryGirl.build (: model, no_author)

> Callback can also be registered globally, but it will mean that they can be ready for any object factories, which are unwanted The cause of side effects can be:

  FactoryGirl.define do after (: build) do | authorable | Authorable.author = build (: user, tenant: official.tenente) end factory: model tenant make {build (: tenant)} final factory: other_model end factoryGirl (Building: Models) #Happiness! To avoid this, you can either wrap the callback into an attribute, such as you have : no_author   Code> Properties, or you can use the factory heritage:  
  Factgeier Define Factory: Tenant Qualified Properties: No-tenant Tenant Zero End Factory: Authorized (do not build) do after; authorable | Authorable.author = build (user, tenant: authorable.tenant) End Symptom: do no_author do (after). Allow Authorable.author = nil end end end end factory: Model, parent :: Authorized, Category: Build 'Model' tenant (Construction :: Tenant)} Final Factory: Other_model end   

Note that to build this work, the code should be clearly specified for the model factory. You can now create an object:

  FactoryGirl.build (: model, no_author) #Happiness! FactoryGirl.build (: other_model) # more joy!   

With another approach, symptoms and callbacks are more viable. When you have a large codebase with many factories, it can actually be the reason for the least unwanted surprise.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -