In this case I'd wrap the framework's membership provider behind an
interface, for example IUserService.
public interface IUserService
{
bool ValidateUser(string username, string password);
}
public class UserService : IUserService
{
public bool ValidateUser(string username, string password)
{
return Membership.ValidateUser(username, password);
}
}
Any place you'd use membership service, instead have a reference to
IUserService and make calls on it instead. This method allows you to
provide a fake value (what we refer to mock or stub) in any code that would
need to use Membership services.
Does that help?
On Sun, Jun 21, 2009 at 10: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:
> > On Sun, Jun 21, 2009 at 11:16 PM, chobo2<[email protected]> wrote:
> > >...
> > > So I tested my validation through unit tests now I am at this part
> >
> > > bool valid = authenticate.VerifyUser(loginValidation.UserName,
> > > loginValidation.Password);
> > > if (valid == false)
> >
> > > So how would I make a mockup that would make sure that valid would be
> > > true?
> >
> > You'd need to be able to replace the authenticate object with a mock
> > object (well, in this case it would technical be a stub, but let's not
> > go into that for now :)). So somewhere in your test code:
> >
> > authenticate = MockRepository.GenerateMock<IAuthentication>();
> > //Replace IAuthentication with whatever the actual class/interface is
> > authenticate
> > .Stub( auth => auth.VerifyUser(Arg<string>.Is.Anything,
> > Arg<string>.Is.Anything))
> > .Return(true);
> >
> > Then when you call your Login() method from your unit test,
> > authenticate will return the value you are expecting.
> >
> > You can also hand-code your mock:
> >
> > class AlwaysVerifiedAuth : IAuthentication {
> > public bool VerifyUser(String username, String password) { return true;
> }
> >
> > }
> >
> > I seem to remember Stephen Walther (http://stephenwalther.com/blog/)
> > having a good series on testing ASP.NET MVC, but can't find the posts
> > at the moment.
> >
> > Regards,
> > David
> >
>
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---