If the Authenticate class is just wrapping the built-in
Membership.ValidateUser() function then you don't need to unit test it
(you'll still obviously do integration tests, manual or automated, to
check it works). Generally you're pretty safe to assume that the
built-in framework code is ok from the perspective of unit testing.

So if you assume that the Authenticate.VerifyUser() method works, then
you just need to test that the rest of your code calls that method
properly. That's where mocking comes in. To make this easy to mock, we
can create an IAuthenticate interface that contains the VerifyUser()
method.

public interface IAuthenticate { bool VerifyUser(string username,
string password); }

We can then manually write a mock for this interface (this is all
typed freehand, so you might need to fix some syntax errors. Basic
idea should be ok though):

public class MockAuthenticate : IAuthenticate {
  public bool ShouldVerify { get; set;}
  public bool VerifyUser(string username, string password) {
    return ShouldVerify;
  }
}

In your unit test of your Login method you'll then need to replace the
IAuthenticate instance being used. Not sure how your code is
structured, but let's assume we can set the IAuthenticate via a
method:

[Test]
public void ShouldRedirectToUrlWhenAuthenticated() {
  //Arrange
  var controller = new LoginController();
  var mockAuthenticate = new MockAuthenticate();
  controller.SetAuthenticator(mockAuthenticate);
  mockAuthenticate.ShouldReturn = true;

  //Act
  var result = controller.Login("someUrl", someForm, true);

  //Assert
  Assert.AreEqual(result.SomeRedirectProperty, "someUrl");
}

If you get sick of writing the mocks by hand, you can use Rhino Mocks
to generate them:

[Test]
public void ShouldRedirectToUrlWhenAuthenticated2() {
  //Arrange
  var controller = new LoginController();
  var mockAuthenticate = MockRepository.GenerateMock<IAuthenticate>();
  controller.SetAuthenticator(mockAuthenticate);
  mockAuthenticate
    .Stub(authenticate =>
authenticate.VerifyUser(Arg<string>.Is.Anything,
Arg<string>.Is.Anything))
    .Return(true);

  //Act
  var result = controller.Login("someUrl", someForm, true);

  //Assert
  Assert.AreEqual(result.SomeRedirectProperty, "someUrl");
}


Hope this helps get you on the right track.

Regards,
David


On Mon, Jun 22, 2009 at 1:50 PM, chobo2<[email protected]> wrote:
>
> Hmm
>
> Ok I will try to do a couple tests by hand to start off with I guess
> to just a get a feel for it. I still don't get how to do them though.
>
> Can someone walk me through like step by step how to test that section
> of my code?
>
> This is the verifyUser method from the authenticate class
>
>   public bool VerifyUser(string userName, string password)
>        {
>            bool valid = Membership.ValidateUser(userName, password);
>            return valid;
>        }
>
> It really does not need to be tested. Otherwise that basically is all
> my code.
>
> On Jun 22, 9:12 am, David Tchepak <[email protected]> wrote:
>> ...

--~--~---------~--~----~------------~-------~--~----~
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