Unit Test not using Mocked object

5 posts, 0 answers
  1. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 12 Aug 2013 Link to this post

    I'm using/implementing JustMock for the first time.  I've watched the webnair, but I can't seem to get the Mocked object working properly.  We have a full paid version of JustMock.

    I have an internal database access layer object, "OperationsData", and an "ComponentException" object with a private constructor.  I'm trying to write the unit test method for the Persist method of the ComponentException object.  The unit tests are all in a separate project so the internal class and the private constructor use a "_Accessor".

    When I run the unit test it complains that OperationsData connection string isn't instantiated.  Why isn't it just using the mocked object and return the expectedExceptionId that I want?  Thanks.

    This is the ComponentException.Persist() ...
    public void Persist()
    {
       try
       {
          OperationsData data = new OperationsData();
          this.ExceptionId = data.PersistException(this.Id, this.ExceptionCode, this.ComponentName, this.SeverityIndicator.ToString(), this.ProcessName, this.BusinessExceptionDetail, this.StackTrace);
       }
       catch (Exception ex)
       {...}
    }
    This is the UnitTest...
    [TestMethod()]
    public void SuccessfulPersistTest()
    {
        /// Arrange
        long expectedExceptionID = 12345;
        long ID = 678901;
        string exceptionCode = string.Empty;
        string componentName = string.Empty;
        string severityIndicator = string.Empty;
        string processName = string.Empty;
        string businessExceptionDetail = string.Empty;
        string stackTrace = string.Empty;
     
        OperationsData_Accessor operationsData = new OperationsData_Accessor();
        operationsData.Arrange(od => od.PersistException(ID, exceptionCode, componentName, severityIndicator, processName, businessExceptionDetail, stackTrace))
            .IgnoreInstance()
            .Returns(expectedExceptionID);
     
        ///Action
        ComponentException_Accessor target = new ComponentException_Accessor();
        target.Persist();
     
        ///Assert
        Assert.AreEqual(expectedExceptionID, target.ExceptionId);
        operationsData.Assert();
    }

    PS.  I also tried...
    OperationsData_Accessor operationsData = Mock.Create<OperationsData_Accessor>(Constructor.Mocked);
  2. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 13 Aug 2013 Link to this post

    Does any one know how to do FutureMocking on an Internal Class using the InternalClass_Accessor?

    This doesn't work...(using MSTest)
    OperationsData_Accessor od = Mock.Create<OperationsData_Accessor>(Constructor.Mocked);
    Mock.NonPublic.Arrange(od, "PersistException", new object[] { _ID, _exceptionCode, _componentName, _severityIndicator, _processName, _businessExceptionDetail, _stackTrace })
        .IgnoreInstance()
        .DoNothing()
        .Returns(_expectedExceptionId)
        .OccursOnce();

    I'm guessing it has something to do with the different between "InternalClass" and "InternalClass_Accessor"
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Aug 2013 Link to this post

    Hello Robert,

    Thank you for contacting our forums.

    As you have noted, it is very possible that the issue is present due to the difference between the InternalClass and its accessor (InternalClass_Accessor).

    To assist you further, I will need a sample project reproducing the matter. You should be able to attach it to the message with your next reply. It will help me to investigate the issue in details and provide a solution faster.

    I hope this is doable for you.

    Thank you for the cooperation in advance.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  5. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 14 Aug 2013 Link to this post

    Support ticket 726528 with sln attached.
  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Aug 2013 Link to this post

    Hi again Robert,

    I have replied in the above mentioned ticket. Please, let me know if the proposed solution helps.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner