Mocking concrete object calls original code

11 posts, 1 answers
  1. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 21 Jul 2010 Link to this post

    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
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 21 Jul 2010 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 21 Jul 2010 Link to this post

    Sure, have you got an email addy I can post it to?
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 21 Jul 2010 Link to this post

    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
  6. Michel Corbin
    Michel Corbin avatar
    20 posts
    Member since:
    Apr 2010

    Posted 24 Jul 2010 Link to this post

    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
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 26 Jul 2010 Link to this post

    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
  8. Chris
    Admin
    Chris avatar
    221 posts

    Posted 26 Jul 2010 Link to this post

    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
  9. Jay
    Jay avatar
    62 posts
    Member since:
    Sep 2005

    Posted 27 Jul 2010 Link to this post

    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.


  10. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 27 Jul 2010 Link to this post

    Code finally uploaded
  11. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 28 Jul 2010 Link to this post

    Got as build that fixed it a treat, thanks!
  12. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 Jul 2010 Link to this post

    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
Back to Top
DevCraft R3 2016 release webinar banner