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

Mocking concrete object calls original code

10 Answers 170 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Imaji
Top achievements
Rank 1
Imaji asked on 21 Jul 2010, 12:35 PM
Hi,

I've need to mock some calls made on a concrete object.

In the beta it was working fine, but I've now upgraded to RTM and the code I wrote to mock out the call is no longer being mocked.  I'm mocking the object like so:

MyConcreteObject stubConcreteObject = Mock.Create<MyConcreteObject>(Behavior.Strict);

And arranging like so:

Mock.Arrange(() => stubConcreteObject.ContainsSomthing(Arg.IsAny<ISomething>())).Returns(true);

But when I run my code (I've tried with the explicit behaviour being set and not setting it), it always goes into the actual code.  This was working in the beta, but appears to be broken now.

Thanks,
John

10 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 21 Jul 2010, 12:52 PM
Hi John,

I fixed a similar issue just after the release and will be available in the next build. Just to make sure that we are on the right track, could please additionally provide a dummy of the class to let me take a look. Also, it will help me to provide you with some workaround.

Regards,
Mehfuz

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Imaji
Top achievements
Rank 1
answered on 21 Jul 2010, 02:15 PM
Sure, have you got an email addy I can post it to?
0
Ricky
Telerik team
answered on 21 Jul 2010, 03:26 PM
Hi John,
Here goes my email address : mehfuz.hossain@telerik.com. Alternatively, you can also attach the class in this ticket.

Regards,
Mehfuz
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Michel Corbin
Top achievements
Rank 1
answered on 25 Jul 2010, 03:38 AM
I have the same issue. The latest release avaiable is  "JustMock Trial Version: 2010.2 713 (Jul 13, 2010)".

Have you build a new version that correct this bug? When can I download it?

Here is my concrete class:

   Public Class FileManager
      Implements IFileManager

      Public Function Delete(ByVal path As String) As Boolean Implements IFileManager.Delete
         System.IO.File.Delete(path)

         Return True
      End Function

      Public Sub Create(ByVal path As String) Implements IFileManager.Create
         System.IO.File.Create(path)
      End Sub
   End Class

And here is my test:

      <Test()> _
      Public Sub TestDelete()
         Dim fileManager As JustMockFile.FileManager

         fileManager = Mock.Create(Of JustMockFile.FileManager)()

         Mock.SetupStatic(Of System.IO.File)(Behavior.Strict)
         Mock.Arrange(Sub() System.IO.File.Delete(Arg.AnyString))
         Mock.Arrange(Function() fileManager.Delete(Arg.AnyString)).Returns(False)

         Assert.IsFalse(fileManager.Delete(Arg.AnyString))
      End Sub

The problem is that instead of just calling a "fake" that return "false", that is trying to call "System.IO.File.Delete" that is define in the FileManager.

The reason why I will pay 400$ for a Mocking framework instead of just use Moq is because theses great features. I'm sorry for that but my boss asks me to hurry to know which framework we will use and I try a lot of things with static methods and concrete class since a couples of days and it's not working.

Please, give me an answer as fast as possible.

Thank you
0
Ricky
Telerik team
answered on 26 Jul 2010, 09:09 AM

Hi Samuel,

File is a framework [mscrolib] class and it can't be mocked as like other static methods. You don’t need to do Mock.SetupStatic(Of System.IO.File)(Behavior.Strict) for File. Therefore, you can directly jump into to the arrrange part [paritial mocking of members]. Also, you need to provide a MockClass attribute on top of your test class.

Finally, you can take a look at the MsCorlibFixture in the Elevated tests in C# examples. You will find examples of mocking File.

Addtionally, there was an issue with static methods for non framework methods.  If you need a build urgently, I can personally send you one. All you have to do is to send a drop location in the previously mentioned email in this thread.

Hope that helps,
Mehfuz



Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chris
Telerik team
answered on 26 Jul 2010, 09:24 AM
Hi Samuel,
Additionally you could open JustMock examples (usually located in \Program Files (x86)\Telerik\JustMock\Example) and open the MsCorlibFixture.cs file.
There you'll find several tests mocking File operations.
e.g.

[TestMethod]
public void ShouldMockStaticFileOperation()
{
Mock.Arrange(() => File.Delete(Arg.IsAny<string>())).DoNothing();
//does nothing
File.Delete("dummy");
}

You just need to make sure that you've marked the test class with the [MockClass] attribute.

Hope this helps.

Regards,
Chris
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jay
Top achievements
Rank 1
answered on 27 Jul 2010, 04:10 PM
I hate to jump onto a question but I'm having the same issue.   I was trying to Mock an Entity Framework object simply by calling:

var context = Mock.Create<DataEntity>(Behavior.Strict);
Mock.Arrange(() => context.History).ReturnsCollection(GetFakeHistory());


But executing the tests throws an exception because the context is trying to connect to the database.  Now I suspect that the issue has more to do with the Entity Framework, if so I'll just tweak the T4 template.  But I thought I'd throw this up here just in case, especially do to the version issue I ran into.  The version on the site says 2010.2.713.17 but the version downloaded is actually 2010.1.713.17 dunno if that's just me, or what.


0
Imaji
Top achievements
Rank 1
answered on 27 Jul 2010, 04:33 PM
Code finally uploaded
0
Imaji
Top achievements
Rank 1
answered on 28 Jul 2010, 01:13 PM
Got as build that fixed it a treat, thanks!
0
Ricky
Telerik team
answered on 29 Jul 2010, 09:34 AM
Hi Guys,

@ John : Nice that things really worked for you finally.

@Jeff : Thanks for notifying us the version mismatch. Uploaded DLL version in site is 2010.1.713. There were few issues with entity framework that is due in the coming build. Additionally you can provide the edmx or repository class here that you are trying to mock so that we can ensure it in advance.


Best wishes,
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Imaji
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Imaji
Top achievements
Rank 1
Michel Corbin
Top achievements
Rank 1
Chris
Telerik team
Jay
Top achievements
Rank 1
Share this question
or