### Install Fixtory Go Package Source: https://github.com/k-yomo/fixtory/blob/master/README.md Use 'go get' to install the latest version of the Fixtory library. ```sh $ go get github.com/k-yomo/fixtory/v2 ``` -------------------------------- ### Build Fixtures with Factory and Blueprints Source: https://github.com/k-yomo/fixtory/blob/master/README.md Use NewBuilder with blueprints to create fixtures. Build2 creates two instances, BuildList creates multiple, and Build creates a single instance. Initializers can override default blueprint values. ```go func TestArticleList_SelectAuthoredBy(t *testing.T) { authorFactory := fixtory.NewFactory(t, Author{}) articleFactory := fixtory.NewFactory(t, Article{}) author1, author2 := authorFactory.NewBuilder(authorBluePrint).Build2() articlesAuthoredBy1 := articleFactory.NewBuilder(articleBluePrint, Article{AuthorID: author1.ID}).BuildList(4) articleAuthoredBy2 := articleFactory.NewBuilder(articleBluePrint, Article{AuthorID: author2.ID}).Build() type args struct { authorID int } tests := []struct { name string list ArticleList args args want ArticleList }{ { name: "returns articles authored by author 1", list: append(articlesAuthoredBy1, articleAuthoredBy2), args: args{authorID: author1.ID}, want: articlesAuthoredBy1, }, { name: "returns articles authored by author 2", list: append(articlesAuthoredBy1, articleAuthoredBy2), args: args{authorID: author2.ID}, want: ArticleList{articleAuthoredBy2}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.list.SelectAuthoredBy(tt.args.authorID); !reflect.DeepEqual(got, tt.want) { t.Errorf("SelectAuthoredBy() = %v, want %v", got, tt.want) } }) } } ``` -------------------------------- ### Define Fixture Blueprints and Factories Source: https://github.com/k-yomo/fixtory/blob/master/README.md Define blueprint functions for your types (Author, Article) and use NewFactory to create factories for them. Blueprints generate default field values. ```go // Author represents article's author type Author struct { ID int Name string } // Article represents article type Article struct { ID int Title string Body string AuthorID int PublishScheduledAt time.Time PublishedAt time.Time Status ArticleStatus LikeCount int } var authorBluePrint = func(i int, last Author) Author { num := i + 1 return Author{ ID: num, Name: fmt.Sprintf("Author %d", num), } } var articleBluePrint = func(i int, last Article) Article { num := i + 1 return Article{ ID: num, Title: fmt.Sprintf("Article %d", i+1), AuthorID: num, PublishScheduledAt: time.Now().Add(-1 * time.Hour), PublishedAt: time.Now().Add(-1 * time.Hour), LikeCount: 15, } } ``` -------------------------------- ### Use EachParam to Override Specific Fixtures in a List Source: https://github.com/k-yomo/fixtory/blob/master/README.md EachParam allows overriding fields for specific fixtures within a generated list. The parameters provided to EachParam correspond to the index of the fixture being overridden. ```go articleFactory := NewArticleFactory(t) articles := articleFactory.NewBuilder(nil, Article{Title: "test article"}) .EachParam(Article{AuthorID: 1}, Article{AuthorID: 2}, Article{AuthorID: 2}) .BuildList(3) ``` -------------------------------- ### Apply Traits to Overwrite Fixture Fields Source: https://github.com/k-yomo/fixtory/blob/master/README.md Traits are used to overwrite specific fields of fixtures. Only non-zero values from traits are applied. Define reusable traits as global variables. ```go // Repeatedly used trait would be better to define as global variable. var articleTraitPublished = Article{ Status: ArticleStatusOpen, PublishScheduledAt: time.Now().Add(-1 * time.Hour), PublishedAt: time.Now().Add(-1 * time.Hour), LikeCount: 15, } // recently published articles articles:= articleFactory.NewBuilder( nil, articleTraitPublished, Article{AuthorID: 5, PublishedAt: time.Now().Add(-1 * time.Minute)}, ).BuildList(2) ``` -------------------------------- ### Overwrite Fields with Zero Value in Go Source: https://github.com/k-yomo/fixtory/blob/master/README.md Use the Zero method to explicitly set fields to their zero value when creating objects. This is useful when the default zero value needs to be applied intentionally. ```go articleFactory := NewArticleFactory(t) // AuthorID will be overwritten with zero value. articles := articleFactory.NewBuilder(articleBluePrint, Article{AuthorID: author1.ID}). Zero(ArticleAuthorIDField). BuildList(4) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.