Faker-cs package for .NET: usage example?

I installed the Faker Port for C # ( https://github.com/oriches/faker-cs ) in my project, but the project site doesnโ€™t give usage examples.

Can someone post some examples of basic mock data generation?

+4
source share
2 answers

In this example, I am using a project that uses MVC4, EF Code First, and Automatic Migrations. Therefore, if you use the same thing, your Migrations\Configuration.cs file should look like this:

 internal sealed class Configuration : DbMigrationsConfiguration<MyProject.Models.MyProjectContext> { public Configuration() { AutomaticMigrationsEnabled = true; } ... 

For individual elements, the example is trivial:

 protected override void Seed(MyProject.Models.MyProjectContext context) { context.Customers.AddOrUpdate( c => c.Name, new Customer { Name = Faker.Name.FullName() } ); context.SaveChanges(); ... 

For a certain number of elements, I liked the idea of โ€‹โ€‹using a lambda expression, like Factory Girl (for Ruby on Rails). In another question I made ( DbMigrations in .NET w / Code First: how to repeat statements with expressions), the answer uses Enumberable.Range() to indicate the number of elements:

 protected override void Seed(MyProject.Models.MyProjectContext context) { context.Companies.AddOrUpdate( p => p.Name, Enumerable.Range(1, 10). Select( x => new Company { Name = Faker.Company.Name() }).ToArray() ); context.SaveChanges(); } ... 
+3
source

There is a shortage of available resources, but this article seems to be what you would expect.

Also try using the Browser Assembly / Object Browser to see available resources (for example, Classes , Methods , etc. are contained in the library). In addition, the library contains NUnit tests, so the code to look at the tests can also be useful.

Since Faker.NET is a port of the Ruby library called ffaker , you can also assume that the code is similar to intentions, so you can also use the ffaker module tests as a small reference.

+1
source

Source: https://habr.com/ru/post/1490199/


All Articles