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

What am I doing wrong?

4 Answers 24 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 25 Oct 2012, 03:44 PM
Here is a unit test setup that I am having issues with....  This code exists within a class called TestBase which my unit test classes inherit from....
#region Mocked Members
 
       protected HardCodedSiteOptionsEntity Target;
       protected IDependencyInjection InjectorMock;
       protected IMessageOperations MessageMock;
 
       #endregion
 
       [SetUp]
       public void InitializeBase()
       {
           Target = Mock.Create<HardCodedSiteOptionsEntity>(Behavior.CallOriginal);
           InjectorMock = Mock.Create<IDependencyInjection>();
           MessageMock = Mock.Create<IMessageOperations>();
 
           Target.Arrange(mockedObject => mockedObject.MessageOperationsHelper).Returns(MessageMock);
 
           //Target.MessageOperationsHelper = MessageMock;
       }

The issue I am having is when I have the code setup as shown I recieve a NullReferenceException on the Target.Arrange method....  I am using the Q3 release of JustMock.

When I comment out the Target.Arrange line and uncomment the line afterwards, it seems to work as expected. 
Here is a snippet of the Target( class under test)

internal class HardCodedSiteOptionsEntity : ISiteOptions, IBlockEntityPartBuilder<uint>
    {
        #region Internal Properties
 
        internal virtual BitArray StatusMaskData { get; set; }
        internal virtual IDependencyInjection InjectorHelper { get; set; }
        internal virtual IMessageOperations MessageOperationsHelper { get; set; }
 
        #endregion
 
        #region Constructor
 
        internal HardCodedSiteOptionsEntity()
        {
            InjectorHelper = new DependencyInjector();
            MessageOperationsHelper = InjectorHelper.InjectEntity<IMessageOperations>();
            StatusMaskData = new BitArray(32, false);
        }
}

I have InternalsVisibleTo attribute set in my assemblyinfo.cs so the internal visibility shouldn't be causing the issue....

4 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 30 Oct 2012, 07:49 PM
Any thoughts?  I was hoping to get a response in here rather than submit a support ticket for this...
0
Ricky
Telerik team
answered on 30 Oct 2012, 09:18 PM
Hi Chris,

Thanks again for reporting the issue. I was able to mock the property in the following way:

[TestInitialize]
     public void InitializeBase()
     {
         Target = Mock.Create<HardCodedSiteOptionsEntity>(Behavior.CallOriginal);
         MessageMock = Mock.Create<IMessageOperations>();
 
         Mock.Arrange(() => Target.MessageOperationsHelper).Returns(MessageMock);
     }
 
     [TestMethod]
     public void Test()
     {
         Assert.IsNotNull(Target.MessageOperationsHelper);
     }

It turns out that there is a bug in fluent interface and will investigate more on that for a possible fix in upcoming SP1. However, the current workaround should work just fine.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Chris
Top achievements
Rank 1
answered on 31 Oct 2012, 02:48 PM
Quick question related to this workaround.

I have experienced an issue in previous verisons of JustMock where the Fluent API and "standard" API's ultimately couldn't mix....  Is that still the case?

For example.

If I mock using the workaround mentioned can I later say MessageMock.Assert() or do I have to go back to the Mock.Assert(MessageMock) format?

I would like to keep using the Fluent API and would prefer not to have to "mix" the two formats if at all possible as far as assertions go between methods... Consistency :)


Thanks.
0
Ricky
Telerik team
answered on 01 Nov 2012, 03:02 PM
Hi Chris,

Thanks again for your reply. 

Under the hood, both standard and fluent API goes through the same workflow. Therefore, it should not be an issue. However, do keep us posted on any such cases.

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
Chris
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or