I'm new to unit testing and the language c sharp and I'm trying to unit test by mocking ISessionStorageService to test the HasGlobaladminaccess function but my setup does not seem to work as expected.
public class Context
{
public asyncTask<bool>HasGlobalAdminAccessAsync(Blazored.SessionStorage.ISessionStorageService sessionStorage)
{
return await HasAccessAsync(sessionStorage, Config.Constants.HAS_GLOBALE_ADMIN_ACCESS);
}
private async Task<bool> HasAccessAsync(Blazored.SessionStorage.ISessionStorageService sessionStorage, string sessionStorageKey)
{
ILogger _logger = Log.ForContext<ContextHelpers>();
try
{
_logger.Debug("HasAccessAsync({sessionStorageKey})", sessionStorageKey);
var access = await sessionStorage.GetItemAsync<bool>(sessionStorageKey);
_logger.Debug("HasAccessAsync({sessionStorageKey})::access={access}", sessionStorageKey, access);
return access;
}
catch (Exception ex)
{
_logger.Debug("HasAccessAsync({sessionStorageKey})::exception={ex}", sessionStorageKey, ex.Message);
return false;
}
}
}
My test method
private readonly Mock<ISessionStorageService> MockStorage = new Mock<ISessionStorageService>();
[Fact()]
public async Task HasGlobalAdminAccessAsyncTest()
{
string guid = System.Guid.NewGuid().ToString();
Context context = new Context();
MockStorage.Setup(foo => foo.GetItemAsync<bool>(guid)).ReturnsAsync(true);
var person = await context.HasGlobalAdminAccessAsync(MockStorage.Object);
Assert.True(person , guid);
}
Assert.True(person, guid)?