Mocked Test Failing

2 posts, 0 answers
  1. Chad
    Chad avatar
    1 posts
    Member since:
    Jan 2007

    Posted 09 May 2012 Link to this post

    I'm trying to test a method that simply looks to see if a value exists in a property that contains a list of strings, but the test fails despite the fact that the value is in the list.  Here is some sample code:

    Imports System.Text
    Imports Telerik.JustMock
     
    Public Class User
     
      Public Sub New()
        MyBase.New()
      End Sub
     
      Public Shared Function Create() As User
        Dim returnValue As New User
        returnValue.UID = "TestUser"
        returnValue.Roles.Add("Admin")
        Return returnValue
      End Function
     
      Public Property UID As String
      Public Property Roles As List(Of String)
     
      Public Function IsAdministrator() As Boolean
        Return _roles.Contains("Admin")
      End Function
     
    End Class
     
     
    <TestClass()>
    Public Class Mocking
     
      <TestMethod()>
      Public Sub Works()
        Dim user As User = user.Create
        Assert.IsTrue(user.IsAdministrator)
      End Sub
     
      <TestMethod()>
      Public Sub DoesNotWork()
     
        Dim user As User = Mock.Create(Of User)()
        Dim roles As New List(Of String)
     
        Mock.Arrange(Function() user.UID).Returns("SomeUser")
        roles.Add("Admin")
        Mock.Arrange(Function() user.Roles).Returns(roles)
     
        Assert.AreEqual("SomeUser", user.UID)
        Assert.AreEqual(roles.Count, user.Roles.Count)
        Assert.AreEqual("Admin", user.Roles.Item(0))
        Assert.IsTrue(user.IsAdministrator)
     
      End Sub
     
    End Class

     

    I'm sure I'm just missing something obvious, so any help is greatly appreciated.
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 14 May 2012 Link to this post

    Hi Chadwick,

    Thanks again for bringing up the question.

    Here I changed the declaration of IsAdministrator method in this way :

    Public Function IsAdministrator() As Boolean
        Return Roles.Contains("Admin")
    End Function

    Instead of using _Roles field, I modified it to use the corresponding property. 

    Next I modified your test in this way:

    Dim user As User = Mock.Create(Of User)(Behavior.CallOriginal)
    Dim roles As New List(Of String)
     
    Mock.Arrange(Function() user.UID).Returns("SomeUser")
    roles.Add("Admin")
    Mock.Arrange(Function() user.Roles).Returns(roles)
     
    Assert.AreEqual("SomeUser", user.UID)
    Assert.AreEqual(roles.Count, user.Roles.Count)
    Assert.AreEqual("Admin", user.Roles.Item(0))
    Assert.IsTrue(user.IsAdministrator)

    During Mock.Create I used the Behavior.CallOriginal argument.  However, if you want to keep your User class untouched then you can just do this:
    Mock.Arrange(Function() user.IsAdministrator).Returns(Function() user.Roles.Contains("Admin"))

    Because you are creating the Mock of User class, calling any member will return default value unless specifically arranged or in other words it will act as a stub.

    Hope this answers your question.

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top