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:
tester.properties.RW = 1
. . .
tester.Run()
. . .
Assert.AreEqual(2, tester.properties.RW)
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.
HTH
Top
|