C# 使用Moq和Xunit测试接口
我是新来的XUnit和Moq。我试图理解测试框架和准备单元测试用例。我使用依赖注入来注入接口 我正在测试以下接口C# 使用Moq和Xunit测试接口,c#,unit-testing,moq,xunit,C#,Unit Testing,Moq,Xunit,我是新来的XUnit和Moq。我试图理解测试框架和准备单元测试用例。我使用依赖注入来注入接口 我正在测试以下接口 using System.Collections.Generic; using Zeiss.IMT.MCCNeo.Settings.Entities; namespace Zeiss.IMT.MCCNeo.Settings.Data.Interface { public interface IProfilesRepository { IList<
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);
}
}
使用System.Collections.Generic;
使用蔡司.IMT.MCCNeo.Settings.Entities;
名称空间Zeiss.IMT.MCCNeo.Settings.Data.Interface
{
公共接口IProfilesRepository
{
IList GetAllProfiles();
IList GetProfilesMatchingUserID(字符串用户ID);
IList GetProfilesForUserIDWithSettingName(字符串用户ID,字符串设置名称);
}
}
实现类
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用蔡司.IMT.MCCNeo.Settings.Data.Interface;
使用蔡司.IMT.MCCNeo.Settings.Entities;
使用System.Linq;
使用蔡司.IMT.MCCNeo.Settings.Data.Singleton;
使用蔡司.IMT.MCCNeo.Settings.Utilities;
命名空间Zeiss.IMT.MCCNeo.Settings.Data.Repository
{
公共类档案库:IProfilesRepository
{
私有IProfileDataRepository_profileDataRepository{get;set;}
公共配置文件存储库(IProfileDataRepository ProfileDataRepository)
{
_profileDataRepository=profileDataRepository;
}
公共IList GetAllProfiles()
{
返回_profileDataRepository.Get();
}
公共IList GetProfilesMatchingUserID(字符串用户ID)
{
if(string.IsNullOrWhiteSpace(userid))抛出新的ArgumentException(“用户Id不能为null”);
返回_profileDataRepository.Get().Where(puid=>puid.UserID.ToLower()==UserID.ToLower()).ToList();
}
公共IList GetProfilesForUserIDWithSettingName(字符串用户ID,字符串设置名称)
{
if(string.IsNullOrWhiteSpace(userid)| string.IsNullOrWhiteSpace(settingname))抛出新的ArgumentException(“用户Id或settingname不能为null”);
var profilesWithSettings=_profileDataRepository.Get()。其中(p=>p.UserID.ToLower()==UserID.ToLower()&p.Settings.Any(s=>s.Name.ToLower()==settingname.ToLower());
返回profilesWithSettings.ToList();
}
}
}
处理数据加载和数据保存到文件的数据存储库
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用蔡司.IMT.MCCNeo.Settings.Entities;
命名空间Zeiss.IMT.MCCNeo.Settings.Data.Singleton
{
公共接口IProfileDataRepository
{
List Get();
列表获取(Func过滤器);
作废保存(列表档案保存);
}
}
使用制度;
使用System.Collections.Generic;
使用蔡司.IMT.MCCNeo.Settings.Entities;
使用蔡司.IMT.MCCNeo.Settings.Utilities;
使用System.Linq;
命名空间Zeiss.IMT.MCCNeo.Settings.Data.Singleton
{
公共类ProfileDataRepository:IProfileDataRepository
{
私有静态列表配置文件;
公共i存储库{get;set;}
公共配置文件数据存储库(IRepository存储库)
{
_存储库=存储库;
if(profiles==null)
{
profiles=repository.Get();
}
}
公共列表Get()
{
返回配置文件;
}
公共列表获取(Func筛选器)
{
返回profiles.Where(filter.ToList();
}
公共作废保存(列表配置文件保存)
{
profiles=profilesToSave;
_保存(配置文件);
}
}
}
我试图模拟概要文件实体,然后将其传递给模拟接口。但是,我仍然不了解如何模拟接口和传递数据实体
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());
}
}
}
实体类
名称空间Zeiss.IMT.MCCNeo.Settings.Entities
{
公共班级简介
{
公共字符串用户标识{get;set;}
公共字符串用户名{get;set;}
公共列表设置{get;set;}
}
}
测试班
使用最小起订量;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用蔡司.IMT.MCCNeo.Settings.Data.Interface;
使用蔡司.IMT.MCCNeo.Settings.Data.Singleton;
使用蔡司.IMT.MCCNeo.Settings.Entities;
名称空间Zeiss.IMT.MCCNeo.Settings.Tests
{
公共类配置文件服务测试
{
私有只读模拟ProfileDataProvider;
私有只读模拟档案提供程序;
公共档案服务测试()
{
ProfileDataProvider=new Mock();
ProfilesProvider=新建模拟();
}
public void GetProfilesMatchingUserID\u当assessedFull\u返回\u异常()时
{
列表设置=新列表(){
新设置(){
Name=“刷新间隔”,
值={},
Type=“string”,
加密=真,
ReadOnly=true,
CreatedOn=new DateTime(),
ModifiedOn=new DateTime(),
Valid=true,
启用=真,
Description=“协议存档视图renwal间隔(秒)。最小值=300最大值=28800”
}
};
配置文件=新配置文件()
{
UserID=“admin”,
UserName=“admin”,
设置=设置
};
列表配置文件=新列表()
{
轮廓
};
ProfileDataProvider.Setup(x=>x.Get())。返回(profiles);
ProfilesProvider.Setup(x=>x.GetProfilesMatchingUserID(null))。返回(新的NullReferenceException());
}
}
}
好心的建议。感觉你想在一堂课上测试所有(或大量)内容。请记住,您还不需要进行集成测试 首先看一下您的
档案库
我们需要像这样的东西
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));
}
}
public类ProfileRepositoryTests
{
//这个班是