Error CS0118 'TestBuilder' is a namespace but is used like a type

I'm working on Unit test. I create TestBuilder class, where I create method SetupTown method. When I tried call this method in my main test class - i have error( Error CS0118'TestBuilder' is a namespace but is used like a type). I read about it and recommend to call a class with method. I tried do it, but It doesn't help.

public partial class TownServiceTests

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

Please check method public TestBuilder SetupTowns

Here my TestClass

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

Could toy tell me what I do wrong?

Example

 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 (here works good)

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

namespace of the file enter image description here

Upvotes: 0

Views: 2360

Answers (2)

saynotoid
saynotoid

Reputation: 1

We dont see any namespaces in your code

Issue is that u have namespace and class named the same

*looking for a way to solve that, alowing to keep the same name

Upvotes: 0

Bogdan Shahnitsky
Bogdan Shahnitsky

Reputation: 792

I think, the issue is happened because you have Something.TestBuilder.Something namespace and compiler is trying to use it instead of class.

You have the TestBuilder folder and a few classes inside it. It may be that classes inside TestBuilder folder contains TestBuilder in their namespaces and compiler trying to access this namespace instead of class.

Upvotes: 0

Related Questions