What am I doing wrong?

5 posts, 0 answers
  1. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 25 Oct 2012 Link to this post

    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....
  2. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 30 Oct 2012 Link to this post

    Any thoughts?  I was hoping to get a response in here rather than submit a support ticket for this...
  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 30 Oct 2012 Link to this post

    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.

  5. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 31 Oct 2012 Link to this post

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

    Posted 01 Nov 2012 Link to this post

    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.

Back to Top
DevCraft R3 2016 release webinar banner