Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3987372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:07:32+00:00 2026-05-20T06:07:32+00:00

I am very new to Unit Testing, so I am starting on my first

  • 0

I am very new to Unit Testing, so I am starting on my first set of tests today. I am using the Library JustMock from Telerik. Though any unit testing information is good. I am having a bit of trouble with an interface service that passes through my method. Below is my MembershipController.Register(model) method…

    [CaptchaValidator]
    [HttpPost]
    public ActionResult Register(Models.Membership.Registration model)
    {
        // just for good mesure, truncate any spaces that are given 
        System.Text.RegularExpressions.Regex.Replace(model.Email, @"\s", "");

        if (ModelState.IsValid)
        {
            // Attempt to register the User and return any applicable status that has to do
            // with the result.
            var createStatus = membershipService.RegisterMember(model.Email, model.Password);

            // if the member is able to be created successfully, log them in now and begin the
            // authentication portion of the registration, otherwise, display the registration
            // errors and return to the view.
            if (createStatus == Membership.MemberCreateStatus.Success)
            {
                formsAuthentication.SignIn(model.Email, false /* createPersistentCookie */);
                return RedirectToAction("Success");
            }
            else
            {
                ModelState.AddModelError("", Membership.Validator.ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

And here is the paltry test I am trying to run…

    [TestMethod]
    public void Register_Post_ReturnsRedirectOnSuccess()
    {
        // Arrange
        var controller = Mock.Create<Web.Controllers.MembershipController>();

        var repository = Mock.Create<Membership.IMembershipService>();

        Mock.Arrange(() => repository.RegisterMember("acceptible@email.com", "acceptiblePassword")).Returns(Membership.MemberCreateStatus.Success);

        // Model
        var model = new Web.Models.Membership.Registration
        {
            Email = "acceptible@email.com",
            Password = "acceptiblePassword",
            ConfirmPassword = "acceptiblePassword"
        };

        // Act
        var result = controller.Register(model);

        // Assert
        Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));

    }

The test fails because membershipService is resolving as null. I’m not sure what to do here. This is my first forray into the Unit Testing aspect of ASP.NET MVC. Can anyone give me some advice?

I am using Ninject to inject IMembershipService through the Constructor. It is implemented by the class MembershipService. The code runs fine when I run it, but the unit tests fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:07:33+00:00Added an answer on May 20, 2026 at 6:07 am

    I don’t see you passing repository anywhere into your controller. Normally you would have IMembershipService as a parameter in your controller’s constructor that you can then pass in when needed or use MVC’s Service Locator to grab the Ninject instance and pass it in.

    :)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to unit testing and writing/using exceptions. I am currently making
I am very new to unit testing even though i have been coding for
I'm pretty new to IoC, Dependency Injection and Unit Testing. I'm starting a new
I am very, very new to unit testing and am trying to write a
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
I am very new to the whole unit testing concept so I'm sorry if
For unit testing you shouldn't test private methods, yes, but for integration tests (using
I am very new to unit testing when it comes to databases and especially
Very new to using Raphael.js I'm looking at the tutorials and I am able
Im very new to all coding including jquery. I though this would have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.