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