Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 模拟WCF客户端的单元测试_Asp.net Mvc_Wcf_Mocking_Nunit - Fatal编程技术网

Asp.net mvc 模拟WCF客户端的单元测试

Asp.net mvc 模拟WCF客户端的单元测试,asp.net-mvc,wcf,mocking,nunit,Asp.net Mvc,Wcf,Mocking,Nunit,我在如何在MVC站点中部署WCF客户端方面遇到了严重的问题,因为MVC站点易于测试。我很难在不实际访问端点的情况下建立服务的模拟 被测站点控制器示例 public class ProfileController : ControllerExtended { public ProfileController(IUserService membershipService, IDropDownService dropdownService, ISiteService siteService)

我在如何在MVC站点中部署WCF客户端方面遇到了严重的问题,因为MVC站点易于测试。我很难在不实际访问端点的情况下建立服务的模拟

被测站点控制器示例

public class ProfileController : ControllerExtended
{

    public ProfileController(IUserService membershipService, IDropDownService dropdownService, ISiteService siteService)
    {
        WCFService.Instance.Client = siteService; //Should maybe be a serpate service.
        _membershipService = membershipService;
        _dropDownService = dropdownService;
        _siteService = siteService;

    }

    public ActionResult Index()
    {
        UserComp profile = _siteService.ProfileGet(_sharedContext.CurrentUser.id);

        return View(new ProfileViewModel { Profile = profile });
    }
}
WCF Singleton我认为我的WCF实现是我的问题,它应该在接口中吗

public sealed class WCFService
{

    public SiteServiceI Client { get; set; }

    #region Singleton

    static readonly WCFService query = new WCFService();

    static WCFService()
    {
    }

    WCFService()
    {
    }

    public static WCFService Instance
    {
        get { return query; }
    }

    #endregion
}
单元测试

[TestFixture]
public class UnitTest1
{
    private Mock<SiteService> mockSiteService;
    private Mock<IUserService> mockMembershipService;
    private Mock<IDropDownService> mockDropDown;
    private Mock<SiteServiceIClient> mockServiceClient; //new Mock<SiteServiceIClient>();


    //private Mock<WebService> mockWebService;

    [SetUp]
    public void SetUp()
    {
        mockSiteService = new Mock<ISiteService>();
        mockMembershipService = new Mock<IUserService>();
        mockDropDown = new Mock<IDropDownService>();
        mockServiceClient = new Mock<SiteServiceIClient>();

        mockWebService = new Mock<WebService>(mockServiceClient);
    }

    [Test]
    public void CheckHomeIndex_Controller()
    {

        var controller = new HomeController(mockMembershipService.Object, mockDropDown.Object, mockPTSearch.Object, mockServiceClient.Object); // mockServiceClient times out.

        Assert.AreEqual("this", "this");
    }
}

我假设发生错误是因为singleton在初始化服务时就设置了它。我试图实现一个单独的服务,纯粹是为了调用WCF,但没有成功地以可测试的方式实现它。不太确定这是否是含糊不清的,但我已经读了很多关于它的文章,而不是一篇文章。

你的问题是你的服务是单例的。单例模式的问题在于它是不可测试的。你应该使用依赖注入来获得服务。

单件无疑是你的问题-使用单件进行测试正在进入一个痛苦的世界。您应该实现一个模式,使这个测试变得友好。大概是这样的:

public interface IServiceFacade
{
    Profile ProfileGet(int id);
}

public class ServiceFacade : IServiceFacade
{

    private WCFService _theRealService = new WCFService();

    public Profile ProfileGet(int id)
    {
        return _theRealService.ProfileGet(id);
    }
}



public class Some_Tests()
{
    public void Test_Stuff_Whatever()
    {

        Mock<IServiceFacade> _facade = new Mock<IServiceFacade>();

        _facade.SetUp(Whatever.....);
    }
}

哪种测试方法更友好?这样我基本上需要添加整个WCF接口的副本本地副本?谢谢,我上车试试看@Oli-是但不是,您在创建的接口中实现了本地副本,但仅从该接口公开您和您的应用程序感兴趣的WCF服务操作。目前,这是第一个访问该服务的应用程序,因此它是一对一的,因为它们是相同的,但感谢您的确认。