我需要模拟数据库连接,然后调用一个方法以返回值。
我创建了最小起订量数据,但不确定如何模拟数据库连接。
layoutData
在下面的控制器类中是所需的参数
<l:VerticalLayout width="100%" >
<HBox>
<IconTabBar>
<items>
<IconTabFilter text="abc" class="Tabheader">
</IconTabFilter>
<IconTabFilter text="fdvvvfv"></IconTabFilter>
<IconTabFilter text="vdfvvdvvv"></IconTabFilter>
<IconTabFilter text="Avfdsd"></IconTabFilter>
</items>
<layoutData>
<FlexItemData growFactor="2" />
</layoutData>
</IconTabBar>
<Button text="Button" class="sapUiSmallMarginTop sapUiTinyMarginBegin" />
</HBox>
</l:VerticalLayout>
需要从列表而不是数据库中获取数据
private void SetupUserRepository()
{
var firstname = "FirstName1";
var _lastName = "LastName1";
var _userName = "FirstName1.LastName1@warburgpincus.com";
var _email = "FirstName1.LastName1@warburgpincus.com";
var User = new User { FirstName = firstname, LastName = _lastName, Username = _userName,Email = _email};
var mockUserRepository = new Mock<IUserRepository>();
// mockUserRepository.Setup(m => m.GetOne(It.IsAny<string>(), It.IsAny<CancellationToken>())).ReturnsAsync((string section, CancellationToken token) => moqData.contentControls.FirstOrDefault(a => a.InternalControlName == section));
// mockUserRepository.Setup(m => m.GetOne(It.IsAny<string>(), It.IsAny<CancellationToken>()));
//var x = moqData.userDetails.First(a => a.Username == _userName);
mockUserRepository.Setup(m => m.GetOne(It.IsAny<string>(), It.IsAny<CancellationToken>())).ReturnsAsync(moqData.userDetails.First(a => a.Username == _userName));
UserRepository test = new UserRepository(mockUserRepository.Object,null,null);
var result = test.GetOne(_userName, It.IsAny<CancellationToken>());
///var repo = new UserRepository(mockUserRepository.Object,null,null);
//var actual = await repo.GetOne(_userName, It.IsAny<CancellationToken>());
Assert.Same(User, result);
}
此行由于期望dbcontext而引发错误。
答案 0 :(得分:0)
您不应该使用UserRepository
的真实实现。而是使用您的IUserRepository
模拟:
var mockUserRepository = new Mock<IUserRepository>();
...
var result = mockUserRepository.Object.GetOne(_userName, It.IsAny<CancellationToken>());