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

Mocked Test Failing

1 Answer 43 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chad
Top achievements
Rank 1
Chad asked on 09 May 2012, 04:21 PM
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.

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 14 May 2012, 04:58 PM
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 >>

Tags
General Discussions
Asked by
Chad
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or