Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNameEditor.EditValue for an invalid provider can throw InvalidCastException #763

Closed
hughbe opened this Issue Apr 13, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@hughbe
Copy link
Contributor

hughbe commented Apr 13, 2019

E.g.

using Moq;

public static IEnumerable<object[]> EditValue_InvalidProvider_TestData()
{
    yield return new object[] { null };

    var nullServiceProviderMock = new Mock<IServiceProvider>(MockBehavior.Strict);
    nullServiceProviderMock
        .Setup(p => p.GetService(typeof(IWindowsFormsEditorService)))
        .Returns(null);
    yield return new object[] { nullServiceProviderMock.Object };

    var invalidServiceProviderMock = new Mock<IServiceProvider>(MockBehavior.Strict);
    invalidServiceProviderMock
        .Setup(p => p.GetService(typeof(IWindowsFormsEditorService)))
        .Returns(new object());
    yield return new object[] { invalidServiceProviderMock.Object };
}

[Theory]
[MemberData(nameof(EditValue_InvalidProvider_TestData))]
public void EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider)
{
    var editor = new FileNameEditor();
    var value = new object();
    Assert.Same(value, editor.EditValue(null, provider, value));
}

If the service is not IWindowsFormsEditorService then an InvalidCastException is thrown.

Proposed fix is to use an as cast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.