如何为singleton类编写nunit测试用例

时间:2017-03-06 09:26:45

标签: class testing singleton nunit case

NAudioEngine是一个单例类,如何编写可以执行以下代码片段:

    private bool CanAddNew(object parameter)
    {
        if (NAudioEngine.Instance.SelectionEnd.Milliseconds != 0)
        {
            return true;
        }
        return false;
    }
   [Test]
    public void AddNewCommandMainVMTestTrue()
    {
        MainVm mainVM = new MainVm();
        RelayCommand command = (RelayCommand)mainVM.AddNewCommand;
        bool canAddNew = command.CanExecute(null);
        Assert.IsTrue(canAddNew);

    }

1 个答案:

答案 0 :(得分:0)

   [Test]
    public void AddNewCommandMainVMTest()
    {
        NAudioEngine.Instance.OpenFile(WAV_FILE);
        NAudioEngine.Instance.SelectionBegin = new TimeSpan(0, 0, 0);
        NAudioEngine.Instance.SelectionEnd = new TimeSpan(0, 0, 0);
        MainVm mainVM = new MainVm();
        RelayCommand command = (RelayCommand)mainVM.AddNewCommand;
        bool canAddNew = command.CanExecute(mainVM);
        Assert.IsFalse(canAddNew);
    }