error cs0118“testbuilder”是一个命名空间,但用作类型

afdcj2ne  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(505)

我在做单元测试。我创建testbuilder类,在其中创建方法setuptown方法。当我试图在我的主测试类中调用这个方法时,我有一个错误(error cs0118'testbuilder'是一个名称空间,但它的用法与类型类似)。我读了相关的文章,建议用方法调用一个类。我试过了,但没用。
公共部分类测试

public partial class TownServiceTests
    {
        private class TestBuilder
        {
            public Mock<ITownRepository> MockTownRepository { get; set; }

            //public Mock<IClientViewModelBuilder> MockClientPropertyModelBuilder { get; set; }

            public Mock<IMapper> MockMapper { get; set; }

            public TestDataGenerator TestDataGenerator;

            private readonly string _jsonDataPath = @"../../../TestData/Town/TownTestData.json";

            private string _jsonDataKey;

            private TownViewModel Towns { get; set; }

            public TestBuilder(string jsonDataKey)
            {
                MockTownRepository = new Mock<ITownRepository>();

                //MockClientPropertyModelBuilder = new Mock<IClientViewModelBuilder>();

                MockMapper = new Mock<IMapper>();

                TestDataGenerator = new TestDataGenerator(_jsonDataPath);

                _jsonDataKey = jsonDataKey;

                TestDataGenerator.LoadData(_jsonDataKey);
            }

            public ITownService Build()
            {
                return new TownService(MockTownRepository.Object,
                                        MockMapper.Object);
            }

            public TestBuilder SetupTowns()
            {
                var towns = TestDataGenerator.GetTestData<Town>(_jsonDataKey, "Towns");

                MockTownRepository.Setup(r => r.InsertTown(It.IsAny<string>()))    
                                    .ReturnsAsync(towns.FirstOrDefault().Id);               

                return this;
            }
        }
    }
}

请检查方法public testbuilder setuptowns
这是我的测试课

[TestClass]
    public partial class TownServiceTests
    {
        [TestMethod]
        public async Task ValidInsertTown()
        {
            var builder = new TestBuilder("Data").SetupTowns; //Problem

            var service = builder.Build();

            var expectedTowns = builder.TestDataGenerator.GetTestData<Town>("Data", "Towns");

            var result = await service.InsertTown(expectedTowns);

            Assert.IsNotNull(result);
            Assert.IsNull(result);
        }
    }

玩具能告诉我我做错了什么吗?
例子

public partial class ClientServiceTests
    {
        private class TestBuilder
        {
            public Mock<IClientRepository> MockClientRepository { get; set; }

            public Mock<IClientViewModelBuilder> MockClientPropertyModelBuilder { get; set; }

            public Mock<IMapper> MockMapper { get; set; }

            public TestDataGenerator TestDataGenerator;

            private readonly string _jsonDataPath = @"../../../TestData/Client/ClientTestData.json";

            private string _jsonDataKey;

            public TestBuilder(string jsonDataKey)
            {
                MockClientRepository = new Mock<IClientRepository>();

                MockClientPropertyModelBuilder = new Mock<IClientViewModelBuilder>();

                MockMapper = new Mock<IMapper>();

                TestDataGenerator = new TestDataGenerator(_jsonDataPath);

                _jsonDataKey = jsonDataKey;

                TestDataGenerator.LoadData(_jsonDataKey);
            }

            public IClientService Build()
            {
                return new ClientService(MockClientRepository.Object
                                       , MockClientPropertyModelBuilder.Object
                                       , MockMapper.Object);
            }

            public TestBuilder SetupClients()
            {
                var clients = TestDataGenerator.GetTestData<ClientSummary>(_jsonDataKey, "Clients");

                MockClientRepository.Setup(r => r.GetClientBySearchCriteria(It.IsAny<string>()))
                                    .ReturnsAsync(clients);

                var clientViewModels = TestDataGenerator.GetTestData<ClientViewModel>(_jsonDataKey, "ClientViewModel");

                MockClientPropertyModelBuilder.Setup(r => r.GetClientViewModel(clients))
                                              .Returns(clientViewModels);

                return this;
            }

            public TestBuilder SetupInvalidInputClients()
            {
                MockClientRepository.Setup(r => r.GetClientBySearchCriteria(It.IsAny<string>()))
                                    .ReturnsAsync(new List<ClientSummary>());

                MockClientPropertyModelBuilder.Setup(r => r.GetClientViewModel(new List<ClientSummary>()))
                                              .Returns(new List<ClientViewModel>());

                return this;
            }
        }
    }

testclass(这里很好用)

[TestMethod]
        public async Task GetClientBySearchCriteria_ValidInput_ReturnClients()
        {
            var searchParameter = "1";

            var builder = new TestBuilder("Data").SetupClients();

            var service = builder.Build();

            var expectedClients = builder.TestDataGenerator.GetTestData<ClientSummary>("Data", "Clients");

            var result = await service.GetClientBySearchCriteria(searchParameter);

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(expectedClients.FirstOrDefault().Name, result.FirstOrDefault().Name);
        }

文件的命名空间

mefy6pfw

mefy6pfw1#

我认为,出现这个问题是因为您有something.testbuilder.something命名空间和编译器试图使用它而不是类。
testbuilder文件夹和一些类在其中。testbuilder文件夹中的类可能包含 TestBuilder 在他们的名称空间和编译器试图访问这个名称空间,而不是类。

相关问题