假 HttpContext.Current.Server.MapPath 用于单元测试

Fake HttpContext.Current.Server.MapPath for unit test

我有一个测试在这条线上失败了。我发现这是因为我的 GetProofOfPurchase 方法中的 HttpContext。这是我失败的行:

var image = Image.GetInstance(HttpContext.Current.Server.MapPath(GlobalConfig.HeaderLogo));

这是我的测试:

 [Test]
    public void GetProofOfPurchase_Should_Call_Get_On_ProofOfPurchaseRepository()
    {
        string customerNumber = "12345";
        string orderNumber = "12345";
        string publicItemNumber = "12345";

    var result = new ProofOfPurchase();
    this.proofOfPurchaseRepository.Expect(p => p.Get(new KeyValuePair<string,string>[0])).IgnoreArguments().Return(result);
    this.promotionTrackerService.GetProofOfPurchase(customerNumber, orderNumber, publicItemNumber);
    this.promotionTrackerRepository.VerifyAllExpectations();
}

promotionTrackerService.GetProofOfPurchase 行测试失败。在这种情况下如何伪造 HttpContext?我在 Stack Overflow 中搜索了与我类似的问题,但我无法解决任何问题。

我试过这样做:

var image = Image.GetInstance(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GlobalConfig.HeaderLogo));

但它没有说:

System.Net.WebException : Could not find a part of the path 'C:\Images\HeaderLogo.png'.

根据我在 Stack Overflow 上读到的内容,如果我打算对其进行单元测试,我不应该使用 HttpContext.Current,这就是我尝试使用 Path.Combine 的原因,但我无法让它正常工作。

有人可以指导我需要做什么才能让这个单元测试正常工作吗?

谢谢!

在为涉及 non-pure 函数的代码编写测试时,我更喜欢做的是将它们隐藏在后面,在最简单的情况下,普通的旧 Func<string, string>:

class PromotionTrackerService
{
    private readonly Func<string, string> imageMapper;

    public PromotionTrackerService(Func<string, string> imageMapper)
    {
        this.imageMapper = imageMapper ?? HttpContext.Current.Server.MapPath;
    }

    public void GetProofOfPurchase()
    {
        var image = Image.GetInstance(imageMapper(GlobalConfig.HeaderLogo));
    }
}

现在,您的测试看起来不像 unit 测试 -- 它更像是一个 integration 测试,具有所有文件访问权限和所有。