【问题标题】:facing issue while tring to fake method in class尝试在课堂上伪造方法时遇到问题
【发布时间】:2016-08-06 02:12:34
【问题描述】:

这是我的控制器测试类方法,它将初始化假类

public void InitializeFake()
{
    fakeHttpSession = A.Fake<HttpSessionStateBase>();
    fakeHttpContext = A.Fake<HttpContextBase>();
    nmsFake = A.Fake<INMSPlatformClient>();

    isFakeInitialized = true;
    fakeHttpSession.Contents["NMCSession"] = new NMCSession()
    {
        DefaultOrganizationUID = 1,
        FirstName = "system",
        Login = "sys.admin",
        LastName = "Admin",
        IsMultiAccountViewable = true,
        OrganizationUID = 1,
        OrganizationName = "NMC",
    };


   var fake = (NMCSession)fakeHttpSession.Contents["NMCSession"];

    A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
    A.CallTo(() => fakeHttpSession["NMCSession"]).Returns(fake);
    A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
    ControllerContext = new ControllerContext(fakeHttpContext, new RouteData(), A.Fake<ControllerBase>());
    myController = new PlatformOrganizationController(nmsFake);
    myController.ControllerContext = ControllerContext;
}

这是我的 NMCSession 课程

public class NMCSession
{
    private List<Product> m_products=null;
    public string Login { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int OrganizationUID { get; set; }
    public string OrganizationName { get; set; }
    public string InstitutionCode { get; set; }
    public int DefaultOrganizationUID { get; set; }

    public string SessionToken { get; set; }

    public int UserUID { get; set; }

    public int NMCInactivityTimeOut { get; set; }

    public bool IsMultiAccountViewable { get; set; }

    public List<Product> GetProducts()
    {
       if(m_products!=null)
           return m_products;
       else
       {
           string NMSPlatformSvcUrl = System.Configuration.ConfigurationManager.AppSettings["NMSServerProtocol"] + "://" +
                                      System.Configuration.ConfigurationManager.AppSettings["NMSServerName"] + ":" +
                                      System.Configuration.ConfigurationManager.AppSettings["NMSServerPortNumber"];
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(NMSPlatformSvcUrl);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = httpClient.GetAsync("/NMS/Platform/ConfigurationSvc/v1/Status").Result;
            if (response.IsSuccessStatusCode)
            {
                PlatformStatus status = response.Content.ReadAsAsync<PlatformStatus>().Result;
                m_products=status.Products;
            }
            else
            {
                throw new Exception(response.StatusCode.ToString());
            }
           rn m_products;
       }
    }

    public bool IsDevelopmentPartner { get; set; }
}

现在问题出在控制器上,用户尝试使用NMCSession.GetProducts(),这给了我异常。

现在我该如何伪造NMCSession中的上述GetProducts()方法调用?

【问题讨论】:

    标签: asp.net-mvc fakeiteasy


    【解决方案1】:

    至少,您需要使用假的NMCSession。在您的测试中,您正在实例化一个具体的NMCSession。另请注意,除非GetProducts 是虚拟的,否则 FakeItEasy 将无法拦截调用,因此将使用原始实现。

    【讨论】:

    • 我试图伪造 NMCSession。请查看编辑后的代码。但我不确定如何获取“GetProducts”的数据。还有其他方法吗?我已经把“GetProducts”方法实现了。
    • 您似乎只是在答案中重复了GetProducts 方法。您仍在制作具体的NMCSession(在fakeHttpSession.Contents["NMCSession"] = new NMCSession() 中),因此调用了原始实现。如果您想伪造NMCSession,则必须制作A.Fake&lt;NMCSession&gt;。为了为GetProducts 提供您自己的行为,GetProducts 必须是虚拟的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2012-04-08
    • 2019-01-03
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多