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

Unit Test not using Mocked object

4 Answers 32 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 12 Aug 2013, 07:14 PM
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);

4 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 13 Aug 2013, 06:26 PM
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"
0
Kaloyan
Telerik team
answered on 14 Aug 2013, 01:35 PM
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.
0
Robert
Top achievements
Rank 1
answered on 14 Aug 2013, 01:56 PM
Support ticket 726528 with sln attached.
0
Kaloyan
Telerik team
answered on 14 Aug 2013, 03:15 PM
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.
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or