When I make this struct a class(
private class _commandLineArgument), everything works as expected. Anybody can explain this behavior? Anyway to make it work without making it a class. Thank you.public abstract class ConsoleBase{ private struct _commandLineArgument { public string Name { get; set; } public string Description { get; set; } public bool IsRequired { get; set; } public string Value { get; set; } public bool Found { get; set; } }}Type argType = Type.GetType( "Utilities.ConsoleBase+_commandLineArgument" );Mock.NonPublic.Arrange<bool>( argType, "Found" ).Returns(true);8 Answers, 1 is accepted
Here's a simplified scenario that still doesn't work as is but works when struct is converted to a class:
public struct commandLineArgument{ public string Name { get; set; } public string Description { get; set; } public bool IsRequired { get; set; } public string Value { get; set; } public bool Found { get; set; }} [Test] public void Test() { commandLineArgument c = Mock.Create<commandLineArgument>(); Mock.Arrange( () => c.Found ).Returns( true ); Assert.IsTrue( c.Found ); }Unfortunately, mocking members from struct is not working properly. However, we found the issue and will try our best to include a fix for it in the upcoming SP release.
In the meantime, you can follow the task here:
http://www.telerik.com/support/pits.aspx#/public/justmock/12182
Kind Regards
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
I have sent you an internal build in ticket #572988. Please check it out.
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
public struct commandLineArgument{ public string Name { get; set; } public string Description { get; set; } public bool IsRequired { get; set; } public string Value { get; set; } public bool Found { get; set; }} [Test] public void Test() { commandLineArgument c = Mock.Create<commandLineArgument>(); Mock.Arrange( () => c.Found ).Returns( true ); Assert.IsTrue( c.Found ); }But the original scenario still throws the "Argument types do not match" error:
public abstract class ConsoleBase{ private struct _commandLineArgument { public string Name { get; set; } public string Description { get; set; } public bool IsRequired { get; set; } public string Value { get; set; } public bool Found { get; set; } }} Type argType = Type.GetType( "Utilities.ConsoleBase+_commandLineArgument" );Mock.NonPublic.Arrange<bool>( argType, "Found" ).Returns(true);Type argType = Type.GetType( "Cmtm.Utilities.ConsoleBase+_commandLineArgument" );var ca = Mock.Create( argType, Constructor.NotMocked, Behavior.CallOriginal );Mock.NonPublic.Arrange<bool>( ca, "Found" ).Returns( true );I get "Attempt by method '_commandLineArgument_Interceptor_1d052085ba9c4ad3bc1b06c9b920f939.Intercept(System.String, _commandLineArgument, Boolean ByRef)' to access type 'Cmtm.Utilities.ConsoleBase+_commandLineArgument' failed." When struct is used in the code being tested.
Again both scenarios work when struct is made into a class. Looks like nested structs are still not supported.
Thanks again for contacting us. However, this is not an issue with nested structure. I changed _commandLineArgument as public and it working as expected:
ConsoleBase._commandLineArgument c = Mock.Create<ConsoleBase._commandLineArgument>();Mock.Arrange(() => c.Found).Returns(true);Assert.IsTrue(c.Found);After doing a bit more debugging, I found that you are trying to mock a non-static property as static. Therefore, I changed the test in the following way and now it is working as expected as well:
[Test]public void Test(){ Type argType = typeof(ConsoleBase).GetNestedType("_commandLineArgument"); var target = Activator.CreateInstance(argType); Mock.NonPublic.Arrange<bool>(target, "Found").Returns(true); bool found = (bool) argType.GetProperty("Found").GetValue(target, null); Assert.IsTrue(found);}Hope this solves your issue. I am using the latest Q2 2012 SP1 build.
Kind RegardsMehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
public abstract class ConsoleBase{ private struct _commandLineArgument { public string Name { get; set; } public string Description { get; set; } public bool IsRequired { get; set; } public string Value { get; set; } public bool Found { get; set; } }}Thanks again for contacting us. I got your point, however at present it is not possible to mock a member from nested private struct.
However, we are investigating it and soon provide a solution.
Kind Regards
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.