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 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: You can just build your items like this After: Note that to build this work, the code should be clearly specified for the 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. 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
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
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
model factory. You can now create an object:
FactoryGirl.build (: model, no_author) #Happiness! FactoryGirl.build (: other_model) # more joy!
Comments
Post a Comment