I have the following Arrange code
This calls a function that does this
(the region manager is returned via a previous call that supplies the mocked region manager, bu that is not germane here, so I have concentrated on the problem area. The access to the region manager sequence is used in several other unit tests, and works fine. When I step thru this in debug, the ProxyRegionManager is in fact the one being used).
I get the mocked region back from oRegionManager just fine. However, when the call to get FirstOrDefault happens, it returns null, NOT the mocked CommandingView.
Am I missing something in the Arrange for invoking FirstOrDefault?
Thanks
var oRegMgr = Mock.Create<
IRegionManager
>();
var oCmdRegion = Mock.Create<
IRegion
>();
var oCmdView = Mock.Create<
CommandingView
>(Constructor.Mocked);
Mock.Arrange(() => oRegMgr.Regions[RegionNames.CommandingRegion]).Returns(oCmdRegion);
Mock.Arrange(() => oCmdRegion.ActiveViews.FirstOrDefault()).Returns(oCmdView);
This calls a function that does this
IRegion commandingRegion = oRegionManager.Regions[RegionNames.CommandingRegion];
CommandingView oCmdView = (CommandingView)commandingRegion.ActiveViews.FirstOrDefault();
(the region manager is returned via a previous call that supplies the mocked region manager, bu that is not germane here, so I have concentrated on the problem area. The access to the region manager sequence is used in several other unit tests, and works fine. When I step thru this in debug, the ProxyRegionManager is in fact the one being used).
I get the mocked region back from oRegionManager just fine. However, when the call to get FirstOrDefault happens, it returns null, NOT the mocked CommandingView.
Am I missing something in the Arrange for invoking FirstOrDefault?
Thanks
5 Answers, 1 is accepted
0
Hi Chuck,
This thread has been replied in your support ticked:
Please do not hesitate to contact us further if you need more help, concerning our product.
Regards,
Kaloyan
the Telerik team
This thread has been replied in your support ticked:
It appears to be a bug in JustMock, which we have already put in our backlog for future fix. You will be notified when we can provide you the solution.
As a workaround to the matter, I could suggest the using of the original FirstOrDefault() implementation, but adding the desired default parameter inside the mocked object property, like this:
var oRegMgr = Mock.Create<IRegionManager>();
var oPrismSL = Mock.Create<IServiceLocator>();
var oSL = Mock.Create<Ingr.SP3D.Common.Client.ServiceLocator>();
var oCmdRegion = Mock.Create<IRegion>();
var oCmdView = Mock.Create<CommandingView>(Constructor.Mocked);
var oCmdViewModel = Mock.Create<CommandingViewModel>(Constructor.Mocked);
var oXmlNode = Mock.Create<XmlNode>();
oCmdRegion.ActiveViews.Add(oCmdView);
Mock.SetupStatic(
typeof
(ClientServiceProviderS3), Behavior.Strict);
Mock.Arrange(() => ClientServiceProviderS3.ServiceLocator).Returns(oSL);
Mock.Arrange(() => oSL.Locator).Returns(oPrismSL);
Mock.Arrange(() => oPrismSL.GetInstance<IRegionManager>()).Returns(oRegMgr);
Mock.Arrange(() => oRegMgr.Regions[RegionNames.CommandingRegion]).Returns(oCmdRegion);
Then when you call the original FirstOrDefault() method you should get the oCmdView returned.
Please do not hesitate to contact us further if you need more help, concerning our product.
Regards,
Kaloyan
the Telerik team
0
Chuck
Top achievements
Rank 1
answered on 06 Feb 2013, 03:48 PM
Hmmm. I wonder if ya'll noticed (as I just discovered) IViewsCollection does not have an Add method.
Apparently Prism just loads up the collection behind the scenes and does not allow the caller to mess with the membership.
Apparently Prism just loads up the collection behind the scenes and does not allow the caller to mess with the membership.
0
Hi Chuck,
Mehfuz
the Telerik team
Thanks again for reaching out to us.
Since ActiveViews is a collection, the recommended way is to mock it via ReturnsCollection that will go in-depth and will let you execute FirstOrDefault or any other Enumerable helper methods on it.
Here is a sample test that I came up with to clarify it further:
[TestMethod]
public
void
TestMethod1()
{
// Arrange
var mock = Mock.Create<Test>();
IList<View> fakesViews =
new
List<View>
{
new
View { Name =
"test"
}
};
Mock.Arrange(() => mock.ActiveViews).ReturnsCollection(fakesViews);
// Act
var actual = mock.ActiveViews.FirstOrDefault();
// Assert
Assert.AreEqual(
"test"
, actual.Name);
}
}
class
Test
{
public
List<View> ActiveViews {
get
;
set
; }
}
public
class
View
{
public
string
Name {
get
;
set
;}
}
Kind Regards
Mehfuz
the Telerik team
0
Chuck
Top achievements
Rank 1
answered on 08 Feb 2013, 04:31 PM
I apologize for being dense, but I do not have ReturnsCollection as an available method (not an Expectation is I think the operative term). When I try to specify this code, ReturnsCollecton is not one of the possible methods. Adding it by hand gives compilation error.
I am using Q3 2012 (2012.3.1016.3) version of JustMock
Mock.Arrange(() => oCmdRegion.ActiveViews). // want to add ReturnsCollection here, but no dice
I am using Q3 2012 (2012.3.1016.3) version of JustMock
0
Hi Chuck,
I managed to configure a test that might work in your case, please give it a try:
Note that, I am able to use ReturnsCollection() on my property.
I hope this helps.
Regards,
Kaloyan
the Telerik team
I managed to configure a test that might work in your case, please give it a try:
// ARRANGE
// Here we mock IRegion in order to be able to arrange expectations later.
IRegion mock = Mock.Create<IRegion>();
// If you need you could use a mocked instance of the CommandingView class.
CommandingView oCmdView =
new
CommandingView();
// Here we add the oCmdView in our fake collection which needs to be from a ViewsCollection type. Later we will call the oCmdView with FirstOrDefault().
ViewsCollection fakesViews =
new
ViewsCollection(
new
ObservableCollection<ItemMetadata>(
new
ItemMetadata[] {
new
ItemMetadata(oCmdView) }), im =>
true
);
// We arrange the mocks AvtiveViews property to return our fake collection.
Mock.Arrange(() => mock.ActiveViews).ReturnsCollection(fakesViews);
// ACT
// We call the getter of the ActiveViews property with FirstOrDefault().
var actual = mock.ActiveViews.FirstOrDefault();
// ASSERT
// We assert that the actual result should be the same as our oCmdView.
Assert.AreEqual(oCmdView, actual);
I hope this helps.
Regards,
Kaloyan
the Telerik team