You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public interface IDataStoreService
{
Task<T> GetDataAsync<T>(string key);
Task SaveDataAsync<T>(string key, T item, bool storeAlways = false);
}
In test create mock object
var mockDataService = new Mock<IDataStoreService>();
mockDataService.Setup(
service => service.SaveDataAsync(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<bool>())).Verifiable();
Act it with this code
var settingsService = new MyStorageService(mockDataService.Object);
var rez = await settingsService.Set("setting", "ok");
Moq throws NullReferenceException on SaveDataAsync call in MyStorageService.
If I change interface method to
Task<bool> SaveDataAsync<T>(string key, T item, bool storeAlways = false);
and change setup to
mockDataService.Setup(
service => service.SaveDataAsync(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<bool>())).ReturnsAsync(true);
all works fine.
The text was updated successfully, but these errors were encountered:
You're not returning a task, therefore, a null is being returned.
If your method signature returns a task, you need to return one ;)
var mockDataService = new Mock<IDataStoreService>();
mockDataService.Setup(
service => service.SaveDataAsync(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<bool>()))
.Returns(Task.FromResult(true)).Verifiable();
Note that a Task is a Task, so you can return that just fine. No need to change the method signature.
Have interface
In test create mock object
Act it with this code
Moq throws NullReferenceException on SaveDataAsync call in MyStorageService.
If I change interface method to
and change setup to
all works fine.
The text was updated successfully, but these errors were encountered: