Mocked method returning null when called from a different project

≡放荡痞女 提交于 2019-12-24 21:54:25

问题


I have this test class:

using NSubstitute;
using NUnit.Framework;
using System;
using System.Linq.Expressions;

namespace MyTests
{    
    public class Tests
    {
        [Test]
        public void Test()
        {
            var companyBL = Substitute.For<ICompanyBL>();

            companyBL.GetCompany(c => new { c.RegionID }).ReturnsForAnyArgs(new
            {
                RegionID = 4,
            });

            var company = companyBL.GetCompany(c => new { c.RegionID });

            var dataRetriever = new DataRetriever(companyBL);
        }
    }
}

and this code in another project:

namespace MyTests
{
    using System;
    using System.Linq.Expressions;

    public interface ICompanyBL
    {
        T GetCompany<T>(Expression<Func<Company, T>> selector);
    }

    public partial class Company
    {
        public int RegionID { get; set; }
    }

    public class DataRetriever
    {
        public DataRetriever(ICompanyBL companyBL)
        {
            //This is null:
            var company = companyBL.GetCompany(c => new
            {
                c.RegionID
            });
        }
    }
}

The company var is null. However, when the code is all contained in the same .cs file in the same project, the value is not null.

Why is the value null when used in another file in another project?

NSubstitute version = 1.10.0.0.

.NET Framework version = 4.5.2.

Issue submitted in Github: https://github.com/nsubstitute/NSubstitute/issues/598


回答1:


I'm not sure how nsubstitute works but I believe you are running into issues where anonymous types only match inside single assembly, they are always different across assemblies.

Roughly you are mocking companyBL.GetCompany<TestAssembly.AnonymousTypeForRegionID> and your tested code calls companyBL.GetCompany<ProductAssembly.AnonymousTypeForRegionID>.

The easiest fix - use some type to pass data shared between those two assemblies. Even Tuple would do. More ideas in Return/consume dynamic anonymous type across assembly boundaries



来源:https://stackoverflow.com/questions/58945013/mocked-method-returning-null-when-called-from-a-different-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!