protected virtual TMyClass GetMockMyClass<TMyClass>(Action<Mock<TMyClass>> setup = null) where TMyClass : class
{
var mockMyClass = new Mock<TMyClass>();
setup?.Invoke(mockMyClass);
return mockMyClass.Object;
}
I need to test this method:
public async Task<FirstDto> FirstAsync(string argument)
{
var result = await this.InternalMethod(argument);
await this.CreateAsync(new Second()
{
// I haven't Id!!!!
Error = result.error,
Argument = argument,
State = result.Success,
});
return result;
}
Here we go!
var myMockMyClass = GetMockMyClass<IMyClass<SecondDto, Second>>(mock =>
{
mock.Setup(x => x.CreateAsync(It.IsAny<Second>()))
.ReturnsAsync(this.mapper.Map<SecondDto>(
/* Set manually the Id (or other attribute) in test SCOPE? */
));
});
await myMockMyClass.FirstAsync("someValue");
// now I need to check that a new register or Second object class...
var entityWithError = await myRepo
.Search<Second>(x => !string.IsNullOrEmpty(x.Error))
.FirstOrDefaultAsync();
In my mock I need to set previously the value of the Id in order to prevent System.NotSupportedException : The property 'Second.Id' does not have a value set and no value generator is available for properties of type 'decimal'. Either set a value for the property before adding the entity or configure a value generator for properties of type 'decimal' in 'OnModelCreating'.
How set the ID in the mock?
CodePudding user response:
