문제

I am unit testing the Dialog boxes and in the Test cases Dialog box may launch with different messages depending upon the Test case .

Dialog box code :

uiService.ShowMessage(StudioViewName.MainWindow, "No cell selected.", this.resourceManager.GetResourceString(StudioResourceManagerName.StudioResourceManager, "IDC_WinshuttleStudio"), MessageBoxButton.OK, MessageBoxImage.Error);

I mocked it like this :UIServicemock.Setup(u=>u.ShowMessage(It.IsAny<int>(),It.IsAny<string>(),It.IsAny<string>(),It.IsAny<MessageBoxButton>(),It.IsAny<MessageBoxImage>()))

Now I want to check the Message in the dialog box or to verify in unit test cases that the particular Message bearing box is popped up only.

도움이 되었습니까?

해결책

You can use a callback to assert that the values match what you expect.

UIServicemock
    .Setup(u => u.ShowMessage(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(),
        It.IsAny<MessageBoxButton>(), It.IsAny<MessageBoxImage>()))
    .Callback<int, string, string, MessageBoxButton, MessageBoxImage>((window, message, error, button, image) => {
        Assert.That(message, Is.EqualTo("No cell selected"));
        Assert.That(window, Is.EqualTo(StudioViewName.MainWindow));
    });

Or you could use the It matchers that match specific parameters, as follows:

UIServicemock
    .Setup(u => u.ShowMessage(
        It.Is<int>(s => s == StudioViewName.MainWindow),
        It.IsIn<string>("No cell selected"),
        It.IsAny<string>(),
        It.IsAny<MessageBoxButton>(),
        It.IsAny<MessageBoxImage>()));

I generally find the first method more flexible, but it is a bit more verbose.

다른 팁

Use Verify:

string expectedResourceString = /* whatever you expect */;
UIServicemock.Verify(u => u.ShowMessage(StudioViewName.MainWindow, 
                                        "No cell selected",
                                        expectedResourceString,
                                        MessageBoxButton.OK, 
                                        MessageBoxImage.Error));

Much clearer what you're trying to test. If you don't care about a value, us It.IsAny in place of it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top