• ABP框架使用(版本3.3.1)


    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));

    Substitue DomainService

        public class ItemManager : DomainService
        {
            public ItemManager()
            {
    
            }
            public virtual  string GetTestData()
            {
                return "GetTestData";
    
            }
    
        }

    Test

            protected override void AfterAddApplication(IServiceCollection services)
            {
                _itemManager = Substitute.For<ItemManager>();
                _itemManager.GetTestData().Returns("MockData");
                services.AddTransient(provider => _itemManager);
            }
    
            [Fact]
            public  void Should_Get_Mock_Data()
            {
                var result = _itemManager.GetTestData();
                result.ShouldBe("MockData");
            }

     但如果DomainService有注入其它参数,就会报错

    Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class:Items.ItemManager.
    Could not find a parameterless constructor.

        public class ItemManager : DomainService
        {
            private IRepository<Item, Guid> _repository;
            public ItemManager(IRepository<Item, Guid> repository)
            {
                _repository = repository;
            }
            public virtual  string GetTestData()
            {
                return "GetTestData";
    
            }
    
        }

    GetRequiredService 获取参数会报错
    System.ArgumentNullException : Value cannot be null. (Parameter 'provider')

    必须将所需的参数Substitue出来传递进去

            protected override void AfterAddApplication(IServiceCollection services)
            {
                // var repository = ServiceProvider.GetRequiredService<IRepository<Item, Guid>>();
                var repository = Substitute.For<IRepository<Item, Guid>>();
                services.AddSingleton(repository);
                _itemManager = Substitute.For<ItemManager>(repository);
                _itemManager.GetTestData().Returns("MockData");
                services.AddTransient(provider => _itemManager);
            }

     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.

  • 相关阅读:
    rm
    Linux下解包/打包,压缩/解压命令
    虚拟机安装---vm12+ubuntukylin16.04
    mysql-5.6.41-winx64安装
    tensorflow学习笔记一------下载安装,配置环境(基于ubuntu16.04 pycharm)
    大一上学期C语言学习心得总结
    常见HTTP状态码
    Java语言基础及java核心
    linux下安装JMeter(小白教程)
    Linux下安装JDK(小白教程)
  • 原文地址:https://www.cnblogs.com/sui84/p/15171111.html
Copyright © 2020-2023  润新知