Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Rhino Mocks AssertWas[Not]Called and Object Properties YAC Software












Rhino Mocks



UI Testing




AssertWas[Not]Called and Object Properties
In the previous texts on AssertWasCalled and AssertWasNotCalled I presented the Rhino Mocks methods for checking whether an object's method was called (or not) during test execution. Checking access to properties is similar for ReadOnly and WriteOnly properties, but different for read/write properties.

Let's consider an interface that declares the 3 different types of properties; and a class that uses that interface:
  Public Interface IProperties
    Property RW As Integer
    ReadOnly Property R As Integer
    WriteOnly Property W As Integer
  End Interface
  Public Class Tester
    Public Property Properties As IProperties
    Public Sub Run()
      If Properties.RW = 1 Then
        Properties.RW = 2
      End If
      If Properties.R = 3 Then
        Properties.RW = 4
      End If
      Properties.W = 5
    End Sub
  End Class
So, we have a read/write property RW, a read-only property R, and a write-only property W. Tester's Run() method will check the values of RW and R, write to RW, and then write to W.

To check a read-only property, we can use AssertWas[Not]Called just like we would with a function:
  properties.AssertWasCalled(Function(its) its.R)
Note that, in the above, only the fact that R's value was accessed can be checked. How it was accessed, in what context, and what happened next - that needs to be checked by verifying values of other properties, calls to methods (or lack thereof), etc.

To check a write-only property, we can use AssertWas[Not]Called just like we would with a sub, just that we have to use an assignment expression with the expected value:
  properties.AssertWasCalled(Sub(its) its.W = 5)
  properties.AssertWasNotCalled(Sub(its) its.W = 6)
Here, you can specify what the expected value assigned to the property was. (BTW, if there are multiple assignments to W in the test run, we can test for all these different values using multiple AssertWasCalled checks).

However, for read/write properties, the above checks will fail; for instance the following code will not work:
  properties.AssertWasCalled(Sub(its) its.RW = 2)
To check a read/write property you should access it directly - from the point of view of the test, it behaves just like any other read/write property. So, you can initialize in before the test, and check its value after the test, using one of the standard Assert methods: = 1
  . . .
  . . .
This means that you cannot check individual reads and writes from/to read/write properties; you can only initialize the property before the test, observe the object's behavior during the test (behavior that depends on different property values), and check the property's final value.



No comments yet...


Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *



Rhino Mocks



Related pages

AssertWasCalled and Methods Called Multiple Times

Rhino Mocks's AssertWasNotCalled

Visual Studio - moving coded UI tests to a new / different project results in null reference exceptions

PrivateObject and Out/ByRef parameters

PrivateObject, WithEvents, and generics

Accessing private members of base classes


PrivateObject and WithEvents

The creator of this fault did not specify a Reason.

Accessing private and protected members - PrivateObject and PrivateType

VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used"

Saving / restoring window placements in .NET

Get the TreeViewItem for an Item in a WPF TreeView

Output in MSTest Tests (VS 2010)

Automated WPF tests and "Invalid URI: Invalid port specified."

Checking Property Change Notifications

Checking "Dangling" Event Handlers in Delphi Forms

Rhino Mocks's AssertWasCalled in VB.NET

First steps with Rhino Mocks (in VB.NET)

Meaningful identifiers

Public fields vs. properties

VS Pending Tests

Automated GUI Testing

Automated GUI Testing in VMs

Automated Testing of Window Forms

Detecting Memory Leaks with DUnit