wandermonk
wandermonk

Reputation: 7346

Using Moq and Xunit to test interfaces

I am new to XUnit and Moq. I am trying to understand both the test frameworks and preparing unit test cases. I am using dependency injection to inject the interfaces.

I am testing the following interfaces

using System.Collections.Generic;
using Zeiss.IMT.MCCNeo.Settings.Entities;

namespace Zeiss.IMT.MCCNeo.Settings.Data.Interface
{
    public interface IProfilesRepository
    {
        IList<Profile> GetAllProfiles();
        IList<Profile> GetProfilesMatchingUserID(string userid);
        IList<Profile> GetProfilesForUserIDWithSettingName(string userid, string settingname);
    }
}

The implementation class

using System;
using System.Collections.Generic;
using Zeiss.IMT.MCCNeo.Settings.Data.Interface;
using Zeiss.IMT.MCCNeo.Settings.Entities;
using System.Linq;
using Zeiss.IMT.MCCNeo.Settings.Data.Singleton;
using Zeiss.IMT.MCCNeo.Settings.Utilities;

namespace Zeiss.IMT.MCCNeo.Settings.Data.Repository
{
    public class ProfilesRepository : IProfilesRepository
    {
        private IProfileDataRepository _profileDataRepository { get; set; }
        public ProfilesRepository(IProfileDataRepository ProfileDataRepository)
        {
            _profileDataRepository = ProfileDataRepository;

        }
        public IList<Profile> GetAllProfiles()
        {
            return _profileDataRepository.Get();
        }
        public IList<Profile> GetProfilesMatchingUserID(string userid)
        {
            if (string.IsNullOrWhiteSpace(userid)) throw new ArgumentException("User Id Cannot be null");
            return _profileDataRepository.Get().Where(puid => puid.UserID.ToLower() == userid.ToLower()).ToList<Profile>();
        }
        public IList<Profile> GetProfilesForUserIDWithSettingName(string userid, string settingname)
        {
            if (string.IsNullOrWhiteSpace(userid) || string.IsNullOrWhiteSpace(settingname)) throw new ArgumentException("User Id or settingname Cannot be null");
            var profilesWithSettings = _profileDataRepository.Get().Where(p => p.UserID.ToLower() == userid.ToLower() & p.Settings.Any(s => s.Name.ToLower() == settingname.ToLower()));
            return profilesWithSettings.ToList();
        }
    }
}

The data repository which deals with data loading and data saving to a file

using System;
using System.Collections.Generic;
using Zeiss.IMT.MCCNeo.Settings.Entities;

namespace Zeiss.IMT.MCCNeo.Settings.Data.Singleton
{
    public interface IProfileDataRepository
    {
        List<Profile> Get();

        List<Profile> Get(Func<Profile, bool> filter);
        void Save(List<Profile> profilesToSave);
    }
}

using System;
using System.Collections.Generic;
using Zeiss.IMT.MCCNeo.Settings.Entities;
using Zeiss.IMT.MCCNeo.Settings.Utilities;
using System.Linq;
namespace Zeiss.IMT.MCCNeo.Settings.Data.Singleton
{
    public class ProfileDataRepository : IProfileDataRepository
    {
        private static List<Profile> profiles;
        public IRepository _repository { get; set; }
        public ProfileDataRepository(IRepository repository)
        {
            _repository = repository;
            if (profiles == null)
            {
                profiles = repository.Get<Profile>();
            }
        }

        public List<Profile> Get()
        {
            return profiles;
        }

        public List<Profile> Get(Func<Profile, bool> filter)
        {
            return profiles.Where(filter).ToList();
        }

        public void Save(List<Profile> profilesToSave)
        {
            profiles = profilesToSave;
            _repository.Save<List<Profile>>(profiles);
        }
    }
}

I am trying to mock the Profile entity and then pass it to the mocked interfaces. But, I am still lacking understanding on how to mock interfaces and pass the data entities.

Entity class

    namespace Zeiss.IMT.MCCNeo.Settings.Entities
    {
        public class Profile
        {
            public string UserID { get; set; }
            public string UserName { get; set; }
            public List<Setting> Settings { get; set; }
        }
    }

Test class

    using Moq;
using System;
using System.Collections.Generic;
using System.Text;
using Zeiss.IMT.MCCNeo.Settings.Data.Interface;
using Zeiss.IMT.MCCNeo.Settings.Data.Singleton;
using Zeiss.IMT.MCCNeo.Settings.Entities;

namespace Zeiss.IMT.MCCNeo.Settings.Tests
{
    public class ProfilesServiceTests
    {
        private readonly Mock<IProfileDataRepository> ProfileDataProvider;
        private readonly Mock<IProfilesRepository> ProfilesProvider;

        public ProfilesServiceTests()
        {
            ProfileDataProvider = new Mock<IProfileDataRepository>();
            ProfilesProvider = new Mock<IProfilesRepository>();
        }

        public void GetProfilesMatchingUserID_WhenPassedNull_Return_Exception()
        {

            List<Setting> settings = new List<Setting>() {
                new Setting(){
                    Name = "RefreshInterval",
                     Value = { },
                     Type = "string",
                Encrypted = true,
                ReadOnly = true,
                CreatedOn = new DateTime(),
                ModifiedOn = new DateTime(),
                Valid = true,
                Enabled = true,
                Description = "Protocol Archive view renwal interval in seconds. Minimum value = 300 Maximum value = 28800"
                }
            };

            Profile profile = new Profile()
            {
                UserID = "admin",
                UserName = "admin",
                Settings = settings
            };

            List<Profile> profiles = new List<Profile>()
            {
                profile
            };

            ProfileDataProvider.Setup(x => x.Get()).Returns(profiles);
            ProfilesProvider.Setup(x => x.GetProfilesMatchingUserID(null)).Returns(new NullReferenceException());

        }
    }
}

Kindly, suggest.

Upvotes: 2

Views: 5988

Answers (1)

Roelant M
Roelant M

Reputation: 1706

It feels like you want to test it all (or a lot) in one class. Keep in mind that you don't need to make integration tests (yet).

First take a look at your ProfilesRepository.

We need something like

public class ProfileRepositoryTests
{
    //this class is only reposible to handle data from the IProfileDataRepository
    private readonly ProfilesRepository _profilesRepository;
    private readonly Mock<IProfileDataRepository> _moqProfileDataProvider;

    public ProfileRepositoryTests()
    {
        _moqProfileDataProvider = new Mock<IProfileDataRepository>();
        _profilesRepository = new ProfilesRepository(_moqProfileDataProvider.Object);
    }

    [Fact]
    public void Get_Succes_NoProfiles()
    {
        _moqProfileDataProvider.Setup(x => x.Get()).Returns(new List<Profile>());

        var profiles = _profilesRepository.GetAllProfiles();

        Assert.AreEqual(0, profiles.Count);
    }

    [Fact]
    public void Get_Succes_AllProfiles()
    {
        _moqProfileDataProvider.Setup(x => x.Get()).Returns(new List<Profile>
        {
            new Profile {UserID = "123"}
        });

        var profiles = _profilesRepository.GetAllProfiles();

        Assert.AreEqual(1, profiles.Count);
        Assert.AreEqual("123", profiles.First().UserID);
        //test more properties
    }

    [Fact]
    public void GetProfilesMatchingUserID_userId_null_Throws_Error()
    {
        Exception ex = Assert.Throws<ArgumentException>(() => _profilesRepository.GetProfilesMatchingUserID(null));    
    }
}

This doesn't complete all tests, but gives you an idea how you can continue. Remember, seperate all class/unit tests etc etc. Each test should check for only one exception. Remember, a unit test only tests one thing, one situation. If your code is throwing two different exceptions, it's can't be doing it under the same conditions.

good luck!

Upvotes: 4

Related Questions