Skip to main content
Version: 1.0.1

Testing

1. Unit Test

Unit Testing is a method of testing the smallest piece of code called a unit. The main goal is to validate that each unit of the software performs as expected. The most important point in Unit Testing, is not to create test cases for everything but to focus on the tests that affect the behavior of the system.

1.1. Milva Assert

Includes custom control methods for the Assert class.

1.1.1. Check Any Items

If none of the members in the collection comply with the predicate the error is thrown.

public static void CheckAnyItems<T>(IEnumerable<T> collection, Predicate<T> predicate)

For example;

MilvaAssert.CheckAnyItems(collection, predicate);

1.1.2. Check Not Any Items

If there are at least 1 item in the collection that meets the predicate it throws an exception.

public static void CheckNotAnyItems<T>(IEnumerable<T> collection, Predicate<T> predicate)

For example;

MilvaAssert.CheckNotAnyItems(collection, predicate);

1.1.3. Check Any Items

If none of the members in the collection comply with the predicate the error is thrown.

public static void CheckAnyItems<T>(T entity, Predicate<T> predicate)

For example;

milvaAssert.CheckAnyItems(entity,predicate);

1.1.4. Check All Items

Performs a check for all members in the entity.
Here you can send properties that need to be checked. See expressions.
Here you can send the rule to be made during the check. See customAssertEnum.

 public static void CheckAllItems<T>(IEnumerable<T> collection, CheckPropertyEnum customAssertEnum, List<Expression<Func<T, object>>> expressions = null)

Another overload;

public static void CheckAllItems<T>(T entity, CheckPropertyEnum customAssertEnum, List<Expression<Func<T, object>>> expressions = null)

For example;

var entities = await _anyService.GetAnyInformationAsync().ConfigureAwait(false);

List<Expression<Func<AnyDTO, object>>> expressions = new List<Expression<Func<AnyDTO, object>>>
{
p => p.Id,
};

var expectedId = 1.ToObjectId();
var expectedCount = 1;

MilvaAssert.CheckAllItems(cat, CheckPropertyEnum.NotDefault, expressions);

Another overload;
Checks whether all members of the collection comply with the predicates.
If one member is incompatible, it throws an error.

public static void CheckAllItems<T>(List<Predicate<T>> predicates, params T[] entities)

For example;

var anyPosts= await _mongoCollection.GetAllAsync(anySpec, anyMethodEnum).ConfigureAwait(false);

MilvaAssert.CheckAllItems(new List<Predicate<AnyEntity>>
{
p => p.Description == "Any Description"
}, anyPosts.ToArray());

Another overload;

public static void CheckAllItems<T>(IEnumerable<T> collection, Predicate<T> predicate)

For example;

MilvaAssert.CheckAllItems(posts.DTOList, p => p.Properties);

1.1.5. Check Exception

This method used Assert.ThrowsAsync(Type, Func{Task}).

public static async Task CheckExceptionAsync(Func<Task> func, string expectedExceptionMessage, Type exceptionType)

For example;

await MilvaAssert.CheckExceptionAsync(func, "CannotGetSignedInUserInfo", typeof(MilvaUserFriendlyException)).ConfigureAwait(false);

1.1.6. Check Not Equal Any Item

Checks whether there is the same element in both lists. If there is at least one same element, it throws an exception.

public static void CheckNotEqualAnyItem<T>(IEnumerable<T> firstCollection, IEnumerable<T> secondCollection)

For example;

MilvaAssert.CheckNotEqualAnyItem(posts.DTOList, postsSecond.DTOList);

1.1.7. Zero

Checks whether the incoming value is zero. If it's not zero, it throws an exception.

public static void Zero(int value)

For example;

MilvaAssert.Zero(1)


2. Integration

Integration tests determine if independently developed units of software work correctly when they are connected to each other.

2.1 Milva Assert

Includes custom control methods for the Assert class.

2.1.1 Check Response For Security

Checks the result of safety tests.

public static void CheckResponseForSecurity(TestExpectected testExpectected, ResponseObject responseObject, bool isAccepted, IStringLocalizer stringLocalizer)

Another Overload;

public static void CheckResponseForSecurity(TestExpectected testExpectected, ObjectResponse<object> objectResponse, bool isAccepted, IStringLocalizer stringLocalizer)

2.1.2 Check Message

Checks messages returned from test results.

public static void CheckMessage(string messageKey, string responseMessage, IStringLocalizer stringLocalizer)

2.1.3 Milva Test Client

Fake client for integration test. | Properties | Description| | :-------------: |:-----:| | AcceptedLanguageIsoCodes | "Supported languages for the project." | | AcceptedRoles | "Supported roles for the project." | | LocalizerResourceSource | "Localizer resource." | | TestApiBaseUrl | "Integration test base url." | | LoginUrl | "Url defined for user input" | | HttpClient | "Http client instance." | | LoginDtoAndUserName | "User input data required for security tests." | | UserManager | "Microsoft.AspNetCore.Identity.UserManager{TUser} instance" | | GetTokenAsync | "Function that returns token for security tests." |

Constructor of MilvaTestClient{TStartup}.

public MilvaTestClient(List<string> acceptedLanguageIsoCodes,
List<string> acceptedRoles,
Type localizerResourceSource,
string testApiBaseUrl,
string loginUrl,
string testEnvironment,
(object, string) loginDtoAndUserName,
Type userManager,
Func<object, Task<string>> getTokenAsync)

2.1.4 Response Object

Response object model for test. | Properties | Description| | :-------------: |:-----:| | StatusCode | "Status code of expected." | | Successful | "Successful of expected." | | Message | "Message key of expected." |

Returns TestExpectected instance.

public static ResponseObject GetTestExpectectedInstance(int? statusCode, bool? isSuccesful, string message)

2.1.5 Safety Test Inject

Helper record for safety tests. | Properties | Description| | :-------------: |:-----:| | Url | "The url to which the request will be sent." | | HttpMethod | "Method type of request." | | Language | "The language in which the request will be sent. " | | StringLocalizer | "Localizer instance for response equals." | | TestExpectected | "Test excepted record." | | IsAcceptedRole | "Whether the role value being tested is accepted" | | Token | "The token to be added when sending the request." |

Returns SafetyTestInject instance.

public static SafetyTestInject GetSafetyTestInject(string url, HttpMethod httpMethod, IStringLocalizer stringLocalizer, string language, TestExpectected testExpectected, bool isAccepted, string token)

2.1.6 Test Expectected

Test result class for safety testing. | Properties | Description| | :-------------: |:-----:| | StatusCode | "Status code of expected." | | MessageKey | "Successful of expected." | | Successful | "Message key of expected." |

Returns TestExpectected instance.

public static TestExpectected GetTestExpectectedInstance(int? statusCode, bool? isSuccesful, string messageKey)

2.1.7 Test Inject

Test result class for safety testing. | Properties | Description| | :-------------: |:-----:| | Url | "The url to which the request will be sent." | | HttpMethod | "Method type of request." | | Language | "The language in which the request will be sent." | | StringLocalizer | "Localizer instance for response equals." | | SpecificObject | "Special object for test methods." |

Returns TestExpectected instance.

public static TestInject GetTestInject(string url, HttpMethod httpMethod, IStringLocalizer stringLocalizer, string language, object specificObject = null)