Unit testing 使用Moq模拟返回IQueryable的存储库<;MyObject>;

Unit testing 使用Moq模拟返回IQueryable的存储库<;MyObject>;,unit-testing,nunit,moq,Unit Testing,Nunit,Moq,如何设置Moq以返回一些值,并让测试的服务选择正确的值 i推定: public interface IGeographicRepository { IQueryable<Country> GetCountries(); } 测试: [测试] public void可以获得正确的国家/地区() { //设置 geographicsRepository.Setup(x=>x.GetCountries()).Returns() //不知道在这里该做什么。 //召唤 var cou

如何设置Moq以返回一些值,并让测试的服务选择正确的值

i推定:

public interface IGeographicRepository
{
    IQueryable<Country> GetCountries();
}
测试:

[测试]
public void可以获得正确的国家/地区()
{
//设置
geographicsRepository.Setup(x=>x.GetCountries()).Returns()
//不知道在这里该做什么。
//召唤
var country=geoService.GetCountry(1);
//应返回属性为CountryName=“牙买加”的对象国家/地区
//断言
声明.声明(国家);
Assert.AreEqual(“牙买加”,国家名称);
Assert.AreEqual(1,country.CountryId);
geographicsRepository.VerifyAll();
}
我基本上被设置卡住了。

你不能使用吗

List countries=新列表();
//添加国家/地区。。。
IQueryable queryableCountries=countries.AsQueryable();
geographicsRepository.Setup(x=>x.GetCountries()).Returns(queryableCountries);

您可以编写一个私人助手方法,该方法将生成Country对象的
IQueryable
,并让您的模拟返回

[Test]
public void Can_Get_Correct_Country()
{
    // some private method
    IQueryable<Country> countries = GetCountries(); 

    //Setup
    geographicsRepository.Setup(x => x.GetCountries()).Returns(countries);

    //Should return object Country with property CountryName="Jamaica"
    //Call
    var country = geoService.GetCountry(1); 

    //Assert
    Assert.IsInstanceOf<Country>(country);
    Assert.AreEqual("Jamaica", country.CountryName);
    Assert.AreEqual(1, country.CountryId);
    geographicsRepository.VerifyAll();
}
[测试]
public void可以获得正确的国家/地区()
{
//一些私人方法
IQueryable countries=GetCountries();
//设置
geographicsRepository.Setup(x=>x.GetCountries()).Returns(countries);
//应返回属性为CountryName=“牙买加”的对象国家/地区
//召唤
var country=geoService.GetCountry(1);
//断言
声明.声明(国家);
Assert.AreEqual(“牙买加”,国家名称);
Assert.AreEqual(1,country.CountryId);
geographicsRepository.VerifyAll();
}

我建议不要使用AsQueryable()。在遇到ORM查询语言上的某些特定方法(Fetch、FetchMany、然后FetchMany、Include、ToFuture等等)之前,它只适用于一些简单的场景

最好使用内存数据库。下面的链接介绍了NHibernate单元测试

我们可以使用标准的RDBMS,也可以使用内存中的数据库,如SQLite,以获得非常快速的测试


解释一下?我对这一切都不熟悉。这也是我模仿IQueryable存储库的方式。这确实使它无法响应ArraySynct这就是他们所说的编写
存根
?所以我必须重写所有返回IQuerable的存储库函数的存根?我是否应该将所有这些存根移动到每个存储库的测试项目中的一个单独文件中?@Shawn:您很可能能够在大多数单元测试中重用相同的存根,不是吗?我会把它保存在你的测试类中。我如何在IQueryable上使用Assert?@Shawn:你想断言什么?
    [Test]
    public void Can_Get_Correct_Country()
    {
        //Setup
        geographicsRepository.Setup(x => x.GetCountries()).Returns()
        //No idea what to do here.

        //Call
        var country = geoService.GetCountry(1); 
        //Should return object Country with property CountryName="Jamaica"

        //Assert
        Assert.IsInstanceOf<Country>(country);
        Assert.AreEqual("Jamaica", country.CountryName);
        Assert.AreEqual(1, country.CountryId);
        geographicsRepository.VerifyAll();
    }
[Test]
public void Can_Get_Correct_Country()
{
    // some private method
    IQueryable<Country> countries = GetCountries(); 

    //Setup
    geographicsRepository.Setup(x => x.GetCountries()).Returns(countries);

    //Should return object Country with property CountryName="Jamaica"
    //Call
    var country = geoService.GetCountry(1); 

    //Assert
    Assert.IsInstanceOf<Country>(country);
    Assert.AreEqual("Jamaica", country.CountryName);
    Assert.AreEqual(1, country.CountryId);
    geographicsRepository.VerifyAll();
}