1. foreign key constraint failed
在TestDataBuilder 中已经加了IdentityUser,但测试Application的时候,还是会报错
TestDataBuilder
private async Task AddUsers() { var adminUser = new IdentityUser(_guidGenerator.Create(), "administrator", "admin@abp.io"); adminUser.AddRole(_adminRole.Id); adminUser.AddClaim(_guidGenerator, new Claim("TestClaimType", "42")); await _userRepository.InsertAsync(adminUser); }
protected override void AfterAddApplication(IServiceCollection services) { _currentUser = Substitute.For<ICurrentUser>(); _currentUser.Id.Returns(_testData.UserJohnId); _currentUser.IsAuthenticated.Returns(true); services.AddSingleton(_currentUser); }
这种方法不起作用
optionsBuilder.UseSqlite($"Filename={databaseName}", x => x.SuppressForeignKeyEnforcement());
可以在连接字符串里指定不用Foreign Keys参数
AbpIdentityEntityFrameworkCoreTestModule
var connection = new SqliteConnection("Data Source=:memory:;Foreign Keys=False");
2. 测试的application方法disable了UOW,会报错Context已经displose
await WithUnitOfWorkAsync(async () => { ... });
3.Substitue 其它
ISettingProvider _settingProvider = Substitute.For<ISettingProvider>(); _settingProvider.GetOrNullAsync(Arg.Any<string>()).Returns((string) null); _settingProvider.GetOrNullAsync(IdentitySettingNames.Password.RequiredLength).Returns(Task.FromResult("42")); services.Replace(ServiceDescriptor.Singleton(_settingProvider));
4.判断报某种Exception
var exception = await Assert.ThrowsAsync<UserFriendlyException>( async () => await _testAppService.UpdateAsync(beforeEntity.Id, testDto) ); exception.Code.Contains("Unique").Should().BeTrue();
5.NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException: Could not find a call to return from.
Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)), and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.