This is a migrated thread and some comments may be shown as answers.

Multiple Interface Mocking

6 Answers 96 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 10 Oct 2012, 02:32 PM
This may already be answered and I am just not searching for the right keywords but I will ask it again....

Within my Architecture I have 2 Interfaces (one public lets call it ISpecificBlockEntity which implements IBlockEntity, and one internal  IBlockEntitySetup).
I then have an object that implements both of these interfaces.  Within my code I have the following generic method:
public TResult CreateEmptyEntity<TResult>() where TResult : IBlockEntity
      {
          var result = DependencyInjector.InjectEntity<TResult>();
 
          var setupEntity = (result as IBlockEntitySetup);
          if (setupEntity == null)
          {
              throw new EntitySetupNotSupportedException(
                  "Entity type requested does not support construction through this method.");
          }
          return result = setupEntity.CreateEmptyEntityā€ˇ<TResult>();
      }

as you can see I don't want the functionality available in IBlockEntitySetup exposed outside of my library so I made that interface internal (the method shown in the entry point into the library).

What I need to do is mock an object for result (which is easy Mock.Create<ISpecificBlockEntity>()), and mock that same object as an IBlockEntitySetup so I can arrange the CreateEmptyEntity method to do nothing (and so setupEntity does not return null).
Here is the unit test for reference....
[Test]
       public void Test0010DependencyInjectionGetMethodCalledOnceWithCorrectType()
       {
           var mockedTower = Mock.Create<ITowerConfiguration>();
           var mockedTowerSetup = mockedTower as IBlockEntitySetup;
 
           Mock.Arrange(() => mockedTowerSetup.CreateEmptyEntity<ITowerConfiguration>()).DoNothing();
           Mock.Arrange(() => InjectorMock.InjectEntity<ITowerConfiguration>()).Returns(mockedTowerSetup as ITowerConfiguration).OccursOnce();
 
           Target.CreateEmptyEntity<ITowerConfiguration>();
 
           Mock.Assert(Target);
       }
This isn't working, it says the profiler must be enabled to mock/asset target IBlockEntitySetup.CreateEmptyEntity() method.
I have also tried replacing mockedTowerSetup with (mockedTower as IBlockEntitySetup) in the arrange statement, but then I get a Not Implemented exception (which is odd since it should be mocked.....).

Debugging for whatever reason isn't working right even with the profiler disabled....

Is there anyway to do this? Am I coming at this from the wrong direction? Any help is greatly appreciated.

6 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 12 Oct 2012, 09:20 PM
Hi Chris,

Thanks again for contacting us. 

However, I would request you to create a sample project and send it over to me so that I can debug further down your issue and send you a solution as soon as possible. 


Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Chris
Top achievements
Rank 1
answered on 15 Oct 2012, 01:39 PM
I have created a very very basic project that I think should help to illustrate the question. The forum won't let me upload a .zip file though....is there an email address I should use or should I create a support ticket?

0
Ricky
Telerik team
answered on 16 Oct 2012, 09:15 AM
Hi Chris,
Thanks again for the sample.

You can send it to mehfuz dot hossain at telerik dot com (made it spam safe). I will check it out and keep you posted on the possible solution.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Chris
Top achievements
Rank 1
answered on 18 Oct 2012, 05:42 PM
all done sent it a few days ago.
0
Chris
Top achievements
Rank 1
answered on 22 Oct 2012, 01:17 PM
Any progress on this question? Is there anything I can do to help? Have I made the question clear or can I provide more information to help clarify the question?
0
Ricky
Telerik team
answered on 22 Oct 2012, 09:17 PM
Hi Chris,

Thanks again for the heads up. I am working on the issue and hopefully will send you an update here in a day or two.


Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Chris
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Chris
Top achievements
Rank 1
Share this question
or