无法实例化类x的代理 - 无法找到无参数构造函数

时间:2017-05-28 19:24:20

标签: c# unit-testing moq mvp

所以我试图在我的应用程序中实现单元测试,并且我试图模拟一些我的构造函数参数来传递。我在这里读到你不应该模拟一个类,而是模拟一个接口,但我也读到,在实现MVP方法时,为演示者实现interface没有多大意义。

我被告知你可以使用这一行将参数传递给构造函数,这也是抛出错误的行:

               var p = new ImpositionCalculatorPresenter(mockView.Object, mockImpositionCalculatorManager.Object, mockSystemVariablesManager.Object,
    mockSystemVariablesPresenter.Object, mockPrintingDesignManager.Object);

任何人都可以帮我实现一种在模拟我的演示者时将参数传递给我的构造函数的方法吗?

   [TestMethod]
        public void CalculateFinalImpositionPlates() {
            //Need to pass in the managers for dependency injection.
            //Then I need to find a way which will trigger the event to call the method on the presenter
            //Then do my tests
            var mockView = new Mock<IImpositionFormView>();

            mockView.SetupProperty(r => r.FinalImpositionOut, 0);

            //These may have to be manually mocked by me so they don't affect JSON file
            var mockImpositionCalculatorManager = new Mock<IImpositionCalculatorManager>();
            var mockSystemVariablesManager = new Mock<ISystemVariablesManager>();
            var mockSystemVariablesPresenter = new Mock<SystemVariablesPresenter>();
            var mockPrintingDesignManager = new Mock<IPrintingDesignManager>();


            //Fix error allowing me to pass in objects to the contstructor
                   var p = new ImpositionCalculatorPresenter(mockView.Object, mockImpositionCalculatorManager.Object, mockSystemVariablesManager.Object,
        mockSystemVariablesPresenter.Object, mockPrintingDesignManager.Object);

    mockView.SetupProperty(r => r.Presenter == p);

            mockView.Raise(r => r.CalculateFinalImpositionPlates += null);

            Assert.Equals(mockView.Object.Plates, 0);

        }

我的演示者的一部分:

public class ImpositionCalculatorPresenter {
    private readonly IImpositionCalculatorManager _impostionFormManager;
    private readonly ISystemVariablesManager _systemVariablesManager;
    private readonly SystemVariablesPresenter _systemVariablesFormPresenter;
    private readonly IPrintingDesignManager _printingDesignManager;
    private readonly string _pathToAppSettings = $"{AppDomain.CurrentDomain.BaseDirectory}/PrintAppSettings.txt";
    private PagePrintingDesignParameters _printingAppParameters;
    private readonly IImpositionFormView _view;

    public ImpositionCalculatorPresenter(IImpositionFormView View, IImpositionCalculatorManager impositionFormManager,
        ISystemVariablesManager systemVariablesManager, SystemVariablesPresenter systemVariablesFormPresenter, IPrintingDesignManager printingDesignManager) {
        _view = View;
        _impostionFormManager = impositionFormManager;
        _systemVariablesManager = systemVariablesManager;
        _systemVariablesFormPresenter = systemVariablesFormPresenter;
        _printingDesignManager = printingDesignManager;
        InitialiseEvents();
    }
    private void InitialiseEvents() {

        _view.CalculateFinalImpositionPlates += CalculateFinalImpositionPlates;


    }

    private void CalculateFinalImpositionPlates(object sender, EventArgs e) {
        //if sheet fed checked, and sheetwise not selected in drop down and sheetsize irregular not select in drop down
        try {
            if (_view.FinalImpositionOut != 0) {
                if (_view.OverridePlates != 0) {
                    _view.Plates = _view.OverridePlates;
                }
                else if (_view.PrintingStyleChecked == PrintingStyle.SheetFed && (_view.CboImposition != "Sheetwise" || _view.CboImposition != "Irregular S\\Wise")) {
                    _view.Plates = _view.ColourSideOne;
                }
                else if (_view.ColourSideOne != 0 && _view.ColourSideTwo != 0) {
                    _view.Plates = _view.ColourSideOne + _view.ColourSideTwo;
                }
            }
        }
        catch (Exception ex) {
            LogErrorToView(this, new ErrorEventArgs(ex.Message));
        }
    }
    }

0 个答案:

没有答案