Skip to content

Commit

Permalink
use bogus for generating test data
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Jul 15, 2023
1 parent 3a2e419 commit 8be4382
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,21 @@ public DepartmentControllerTests(TestWebApi api)
{
_api = api;
}

[Fact]
public async Task SearchDepartments_ReturnsExpectedDepartments()
{
//Arrange
_api.SeedData(context =>
{
context.Departments.Add(new Department()
{
Id = 0,
Name = "Foo",
City = "bar",
});
});

var department = Fakers.CreateDepartmentFaker().Generate();
_api.SeedData(context => { context.Departments.Add(department); });

//Act
var result = await _api.CreateClient().GetFromJsonAsync<DepartmentDto[]>("Department");

//Assert
result.Should().BeEquivalentTo(new[]
{
new DepartmentDto()
{
Id = 0,
Name = "Foo",
City = "bar",
}
department.ToDto()
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ public async Task UpdateEmployee_IsUpdated()
{
//Arrange
var employee = Fakers.CreateEmployeeFaker().Generate();
_api.SeedData(context =>
{
context.Employees.Add(employee);
});

_api.SeedData(context => { context.Employees.Add(employee); });

var updatedEmployee = employee.ToDto() with
{
FirstName = "Updated",
Expand All @@ -90,10 +87,7 @@ public async Task DeleteEmployee_IsDeleted()
{
//Arrange
var employee = Fakers.CreateEmployeeFaker().Generate();
_api.SeedData(context =>
{
context.Employees.Add(employee);
});
_api.SeedData(context => { context.Employees.Add(employee); });

//Act
var result = await _api.CreateClient().DeleteAsync($"Employee/{employee.Id}");
Expand Down
37 changes: 5 additions & 32 deletions CleanAspCore.Api.Tests/Features/Import/ImportControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ public ImportControllerTests(TestWebApi api)
[Fact]
public async Task Import_SingleNewEmployee_IsImported()
{
var employee = Fakers.CreateEmployeeFaker().Generate();
_api.ConfigureServices(services =>
{
var fileProviderMock = new Mock<IFileProvider>()
.SetupJsonFileMock("TestData/Employee.json", new[]
{
new EmployeeDto()
{
Id = 1,
FirstName = "Foo",
LastName = "Bar",
Email = "email@foobar.com",
Gender = "Weird",
JobId = 2,
DepartmentId = 3
}
employee.ToDto()
})
.SetupJsonFileMock("TestData/Job.json", Array.Empty<JobDto>())
.SetupJsonFileMock("TestData/Department.json", Array.Empty<DepartmentDto>());
Expand All @@ -43,18 +35,8 @@ public async Task Import_SingleNewEmployee_IsImported()

_api.SeedData(context =>
{
context.Jobs.Add(new Job
{
Id = 2,
Name = "Foo",
});
context.Departments.Add(new Department
{
Id = 3,
Name = "Bar",
City = "Foo"
});
context.Jobs.Add(employee.Job);
context.Departments.Add(employee.Department);
});

//Act
Expand All @@ -66,16 +48,7 @@ public async Task Import_SingleNewEmployee_IsImported()
{
context.Employees.Should().BeEquivalentTo(new []
{
new Employee
{
Id = 1,
FirstName = "Foo",
LastName = "Bar",
Email = "email@foobar.com",
Gender = "Weird",
JobId = 2,
DepartmentId = 3
}
employee
});
});
}
Expand Down
18 changes: 4 additions & 14 deletions CleanAspCore.Api.Tests/Features/Jobs/JobControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,21 @@ public JobControllerTests(TestWebApi api)
_api = api;
}


[Fact]
public async Task SearchJobs_ReturnsExpectedJobs()
{
//Arrange
_api.SeedData(context =>
{
context.Jobs.Add(new Job()
{
Id = 0,
Name = "Foo",
});
});
var job = Fakers.CreateJobFaker().Generate();
_api.SeedData(context => { context.Jobs.Add(job); });

//Act
var result = await _api.CreateClient().GetFromJsonAsync<JobDto[]>("Job");

//Assert
result.Should().BeEquivalentTo(new[]
{
new JobDto()
{
Id = 0,
Name = "Foo",
}
job.ToDto()
});
}
}

0 comments on commit 8be4382

Please sign in to comment.