Hi
I have some kind of tricky bug..
I have private method which I fake with Mock.NonPublic.Arrange and it returns "TestValueFoo" string
I debug .. everything goes ok .. this "TestValueFoo " was added to list in real code..and which I check in test code later .. In test code there is no "TestValueFoo "
if I change tested method to public and then I use Mock.Arrange(...and it returns "TestValueFoo"
Then this same code works fine in test method
so there is some kind of problem with private mocking
Debug show that "TestValueFoo" was added to list but when program goes back to test code then there is no "TestValueFoo" in list anymore
visual studio 2010 and visual studio unit test
Hopeful you got the idea.. which I was trying to explain
I have some kind of tricky bug..
I have private method which I fake with Mock.NonPublic.Arrange and it returns "TestValueFoo" string
I debug .. everything goes ok .. this "TestValueFoo " was added to list in real code..and which I check in test code later .. In test code there is no "TestValueFoo "
if I change tested method to public and then I use Mock.Arrange(...and it returns "TestValueFoo"
Then this same code works fine in test method
so there is some kind of problem with private mocking
Debug show that "TestValueFoo" was added to list but when program goes back to test code then there is no "TestValueFoo" in list anymore
visual studio 2010 and visual studio unit test
Hopeful you got the idea.. which I was trying to explain
9 Answers, 1 is accepted
0
Bob
Top achievements
Rank 1
answered on 28 Mar 2012, 01:29 PM
Wouldn't it be a good idea to post a code snippet demonstrating the issue you are trying to explain?
0
ari
Top achievements
Rank 1
answered on 28 Mar 2012, 01:48 PM
[TestMethod]
public void InternalGetAll_ListDHPDocumentClass()
{
Boolean called = false;
DHPDocumentClassWorker documentClassWorker = CreateWorker<DHPDocumentClassWorker>();
// Mock needed method for worker to use from entity context
Mock.Arrange(() => DBLayer.GetDocumentClasses()).ReturnsCollection(CreateDocumentClassList());
Mock.Arrange(() => documentClassWorker.RetList).ReturnsCollection(CreateDocumentClassList()); //CreateDocumentClassList = IEnumerable<DHPDocumentClass>
Mock.NonPublic.Arrange(documentClassWorker, "OrderChildItems", ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<Int32>(), ArgExpr.IsAny<Int32>()).DoInstead(() => called = true);
// TODO have to check why this is not workig,, Actual it works .. but in code something strange happens,,,"testi" was not added to name
//Mock.NonPublic.Arrange<string>(DocumentClassWorker, "GetDocumentClassFullName", ArgExpr.IsAny<Int64>(), ArgExpr.IsAny<List<DHPDocumentClass>>()).Returns("TestValueFoo"); --> this does not work
// that's why GetDocumentClassFullName was changed to public -->
Mock.Arrange(() => documentClassWorker.GetDocumentClassFullName(Arg.IsAny<Int32>(), Arg.IsAny<List<DHPDocumentClass>>())).Returns("TestValueFoo");
// Execute method in test
IList<DHPDocumentClass> x = documentClassWorker.InternalGetAll();
Assert.AreEqual(x.Count(), 2);
Assert.AreEqual(x[0].Name, "TestValueFoo");
//Here now it's samame value as it is set in CreateDocumentClassList but it shoud be TestValueFoo which value comes from mock
// Test does not work with Mock.NonPublic
Assert.AreEqual(x[1].Name, "TestValueFoo");
}
Real Test Code
public IList<DHPDocumentClass> InternalGetAll()
{
List<DHPDocumentClass> allClassesList = ctx.DHP.GetDocumentClasses().ToList();
List<DHPDocumentClass> parentClasses = allClassesList.Where(c => c.ParentID == null).OrderBy(o => o.Name).ToList();
List<DHPDocumentClass> retList = new List<DHPDocumentClass>();
// This is for juctmock test
retList = RetList;
// Create sort order by iterating through child items
Int32 orderNum = 0;
Int32 orderLevel = 0;
OrderChildItems(allClassesList, parentClasses, retList, orderNum, orderLevel);
// Update full names
foreach (var documentClass in retList)
{
documentClass.FullName = GetDocumentClassFullName(documentClass.DocumentClassID, retList); //mocked
// Here documentClass.FullName = same value as it's mocked.. !!!!
}
return retList.OrderBy(o => o.UIOrder).ToList();
}
public void InternalGetAll_ListDHPDocumentClass()
{
Boolean called = false;
DHPDocumentClassWorker documentClassWorker = CreateWorker<DHPDocumentClassWorker>();
// Mock needed method for worker to use from entity context
Mock.Arrange(() => DBLayer.GetDocumentClasses()).ReturnsCollection(CreateDocumentClassList());
Mock.Arrange(() => documentClassWorker.RetList).ReturnsCollection(CreateDocumentClassList()); //CreateDocumentClassList = IEnumerable<DHPDocumentClass>
Mock.NonPublic.Arrange(documentClassWorker, "OrderChildItems", ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<List<DHPDocumentClass>>(), ArgExpr.IsAny<Int32>(), ArgExpr.IsAny<Int32>()).DoInstead(() => called = true);
// TODO have to check why this is not workig,, Actual it works .. but in code something strange happens,,,"testi" was not added to name
//Mock.NonPublic.Arrange<string>(DocumentClassWorker, "GetDocumentClassFullName", ArgExpr.IsAny<Int64>(), ArgExpr.IsAny<List<DHPDocumentClass>>()).Returns("TestValueFoo"); --> this does not work
// that's why GetDocumentClassFullName was changed to public -->
Mock.Arrange(() => documentClassWorker.GetDocumentClassFullName(Arg.IsAny<Int32>(), Arg.IsAny<List<DHPDocumentClass>>())).Returns("TestValueFoo");
// Execute method in test
IList<DHPDocumentClass> x = documentClassWorker.InternalGetAll();
Assert.AreEqual(x.Count(), 2);
Assert.AreEqual(x[0].Name, "TestValueFoo");
//Here now it's samame value as it is set in CreateDocumentClassList but it shoud be TestValueFoo which value comes from mock
// Test does not work with Mock.NonPublic
Assert.AreEqual(x[1].Name, "TestValueFoo");
}
Real Test Code
public IList<DHPDocumentClass> InternalGetAll()
{
List<DHPDocumentClass> allClassesList = ctx.DHP.GetDocumentClasses().ToList();
List<DHPDocumentClass> parentClasses = allClassesList.Where(c => c.ParentID == null).OrderBy(o => o.Name).ToList();
List<DHPDocumentClass> retList = new List<DHPDocumentClass>();
// This is for juctmock test
retList = RetList;
// Create sort order by iterating through child items
Int32 orderNum = 0;
Int32 orderLevel = 0;
OrderChildItems(allClassesList, parentClasses, retList, orderNum, orderLevel);
// Update full names
foreach (var documentClass in retList)
{
documentClass.FullName = GetDocumentClassFullName(documentClass.DocumentClassID, retList); //mocked
// Here documentClass.FullName = same value as it's mocked.. !!!!
}
return retList.OrderBy(o => o.UIOrder).ToList();
}
0
Hi Ari,
Thanks again for sending the issue. However, please check that the argument type which is used in ArgExpr.IsAny is correct. Here I can see that you have used Int64 / long in the non-public setup which is not the same with public setup.
Also, please send me the method signature of GetDocumentClassFullName if converting from ArgExpr.IsAny<Int64> to ArgExpr.IsAny<Int32> does not work.
Kind Regards
Mehfuz
the Telerik team
Thanks again for sending the issue. However, please check that the argument type which is used in ArgExpr.IsAny is correct. Here I can see that you have used Int64 / long in the non-public setup which is not the same with public setup.
//Mock.NonPublic.Arrange<string>(DocumentClassWorker, "GetDocumentClassFullName", ArgExpr.IsAny<Int64>(), ArgExpr.IsAny<List<DHPDocumentClass>>()).Returns("TestValueFoo"); --> this does not work
// that's why GetDocumentClassFullName was changed to public -->
Mock.Arrange(() => documentClassWorker.GetDocumentClassFullName(Arg.IsAny<Int32>(), Arg.IsAny<List<DHPDocumentClass>>())).Returns(
"TestValueFoo"
);
Also, please send me the method signature of GetDocumentClassFullName if converting from ArgExpr.IsAny<Int64> to ArgExpr.IsAny<Int32> does not work.
Kind Regards
Mehfuz
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
ari
Top achievements
Rank 1
answered on 02 Apr 2012, 07:47 AM
Hi
it did not help ...
here is code... this is mocked... and it have to be: ArgExpr.IsAny<Int64>()
private String GetDocumentClassFullName(Int64 documentClassID, IEnumerable<DHPDocumentClass> classList)
{
String retval = String.Empty;
DHPDocumentClass documentClass = classList.Where(c => c.DocumentClassID == documentClassID).Single();
if (documentClass.ParentID.HasValue && documentClass.ParentID > 0)
{
retval = GetDocumentClassFullName(documentClass.ParentID.Value, classList) + " / " + documentClass.Name;
}
else
{
retval = documentClass.Name;
}
return retval;
}
it did not help ...
here is code... this is mocked... and it have to be: ArgExpr.IsAny<Int64>()
private String GetDocumentClassFullName(Int64 documentClassID, IEnumerable<DHPDocumentClass> classList)
{
String retval = String.Empty;
DHPDocumentClass documentClass = classList.Where(c => c.DocumentClassID == documentClassID).Single();
if (documentClass.ParentID.HasValue && documentClass.ParentID > 0)
{
retval = GetDocumentClassFullName(documentClass.ParentID.Value, classList) + " / " + documentClass.Name;
}
else
{
retval = documentClass.Name;
}
return retval;
}
0
Hi Ari,
Thanks again for the reply. I created a sample project with the method that you have pasted above and it is working as expected. Please update the project to the point where you are getting the exception and send it back to me so that I can investigate further.
Here to note that I am using the latest version of JustMock.
Kind Regards
Mehfuz
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
ari
Top achievements
Rank 1
answered on 03 Apr 2012, 10:48 AM
Hi
I edited project points where error comes and where it goes right..
so in test it's wrong ..as long as it's in code it,s ok but in test it's wrong
I have trial version 2012.1.215.4
We are going to buy this if My tests go through
I edited project points where error comes and where it goes right..
so in test it's wrong ..as long as it's in code it,s ok but in test it's wrong
I have trial version 2012.1.215.4
We are going to buy this if My tests go through
0
Hi Ari,
It’s great that you are able to reproduce the problem. I would request you to send me the updated sample that I sent you with the exception you reproduced.
On your second question, I actually didn't understand by "as long as it's in code its ok but in test it's wrong". It would be great if you elaborate it a bit further.
Kind Regards
Mehfuz
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
ari
Top achievements
Rank 1
answered on 04 Apr 2012, 06:50 AM
hi your example works fine ..
but this
On your second question, I actually didn't understand by "as long as it's in code its ok but in test it's wrong". It would be great if you elaborate it a bit further.
in code I mean the code that i am going to test...
and when mocked result comes to test .. it's wrong ( With non.public.arrange) // there is text that show where it goes wrong
BUT IT's fine with mock.arrange.. if i change method to public..but That is what I do not want to do
but this
On your second question, I actually didn't understand by "as long as it's in code its ok but in test it's wrong". It would be great if you elaborate it a bit further.
in code I mean the code that i am going to test...
and when mocked result comes to test .. it's wrong ( With non.public.arrange) // there is text that show where it goes wrong
BUT IT's fine with mock.arrange.. if i change method to public..but That is what I do not want to do
0
ari
Top achievements
Rank 1
answered on 04 Apr 2012, 08:48 AM
Hi
I fouded bug in my test .. so this case is closed
Thanks
I fouded bug in my test .. so this case is closed
Thanks