The test fails for the exact reason stated: Rhino mocks expected one
call to IsendEmail.IsValid("[email protected]") but it actually only
saw zero.

In your EmailHelper class, you're calling the class' IsValid instead
of the IsValid on the IsendEmail instance you passed in on the
constructor (the mock).  Instead of calling smtpClient.IsValid, you're
only calling isValid, which will call the current instance's isValid
(basically, Me.isValid or "this.isValid" in C#).

---
Patrick Steele
http://weblogs.asp.net/psteele



On Wed, Mar 24, 2010 at 8:16 PM, Li Aishen <[email protected]> wrote:
> Can anyone help me on  why this test fail? I got error like:
> Test method TestwwwrootWAP.sendEmailTest.SendTest threw exception:
> Rhino.Mocks.Exceptions.ExpectationViolationException:
> IsendEmail.IsValid("[email protected]"); Expected #1, Actual #0.
>
> Trace Stack:
> Rhino.Mocks.RhinoMocksExtensions.AssertWasCalled[T](T mock, Action`1
> action, Action`1 setupConstraints)
> Rhino.Mocks.RhinoMocksExtensions.AssertWasCalled[T](T mock, Func`2
> action)
>
> This is the test code:
>  Public Sub SendTest()
>
>        'arrange
>        Dim sender As IsendEmail = MockRepository.GenerateMock(Of
> IsendEmail)()
>        Dim toEmail As String = "[email protected]"
>        Dim fromEmail As String = "[email protected]"
>        Dim subject As String = "using mock"
>        Dim msg As String = "test the behavior"
>        'act
>        Dim target As EmailHelper = New EmailHelper(sender)
>        sender.Stub(Function(x) x.IsValid(toEmail)).[Return](True)
>        sender.Stub(Function(x) x.IsValid(fromEmail)).[Return](True)
>        Dim actual As Boolean = target.Send(toEmail, fromEmail,
> subject, msg)
>
>        'assert
>        sender.AssertWasCalled(Function(x) x.IsValid(toEmail))
>        sender.AssertWasCalled(Function(x) x.IsValid(fromEmail))
>        Assert.AreEqual(True, actual)
>
>    End Sub
> Here is the under test class:
>
> Imports Microsoft.VisualBasic
> Imports System.Net.Mail
> Imports System.Text.RegularExpressions
>
> Public Class EmailHelper
>    Implements IsendEmail
>
>    Private smtpClient As IsendEmail
>
>    Public Sub New(ByRef smtpClient As IsendEmail)
>        Me.smtpClient = smtpClient
>    End Sub
>
>
>    Public Function Send(ByVal ToEmail As String, ByVal FromEmail As
> String, ByVal StrSubject As String, ByVal StrMsg As String) As Boolean
> Implements IsendEmail.Send
>        Dim result As Boolean = True
>        Try
>            If IsValid(ToEmail) And IsValid(FromEmail) Then
>
>                Me.smtpClient.Send(ToEmail, FromEmail, StrSubject,
> StrMsg)
>
>                result = True
>
>            End If
>            Return result
>        Catch ex As Exception
>            Throw ex
>        End Try
>
>    End Function
>
>
>
>    Public Function IsValid(ByVal emailAddress As String) As Boolean
> Implements IsendEmail.IsValid
>        Return Regex.IsMatch(emailAddress, "^([0-9a-zA-Z]([-\.
> \w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
>    End Function
>
> End Class
> and interface is:
> Imports System.Net.Mail
>
> Public Interface IsendEmail
>    Function Send(ByVal ToEmail As String, ByVal FromEmail As String,
> ByVal StrSubject As String, ByVal StrMsg As String) As Boolean
>    Function IsValid(ByVal email As String) As Boolean
> End Interface
> Thank you
> Li Aishen
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Rhino.Mocks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rhinomocks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to