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 7966629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:28:50+00:00 2026-06-04T06:28:50+00:00

I have this controller: [HttpPost] public ActionResult Create(Company company) { // try to save

  • 0

I have this controller:

[HttpPost]
        public  ActionResult Create(Company company)
        {

            // try to save the record
            if (ModelState.IsValid)
            {

                // create the command
                var command = new CreateOrUpdateCompanyCommand
                {
                    CompanyId = company.CompanyId,
                    Code = company.Code,
                    Name = company.Name
                };

                // check for errors
                IEnumerable<ValidationResult> errors = _commandBus.Validate(command);

                ModelState.AddModelErrors(errors);

                if (ModelState.IsValid)
                {
                    var result = _commandBus.Submit(command);
                    if (result.Success) 
                        return RedirectToAction("Index");
                }

            }

            // if fail
            return View("Create", company);

        }

I have this test for NUnit:

[Test]
        public void Create()
        {

            const string expectedRouteName = "Index";

            // Mock the arguments
            var mockRepository = Substitute.For<ICompanyRepository>();
            var mockProcessor = Substitute.For<ICommandBus>();

            // Arrange
            var controller = new CompanyController(mockProcessor, mockRepository);

            // Act
            var company = new Company
            {
                Code = "XXXXXXX",
                CompanyId = 1,
                Name = "Sample company"
            };

            var result = controller.Create(company) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result, "Should return a ViewResult");
            Assert.AreEqual(expectedRouteName, result.RouteValues["action"], 
                "View name should have been '{0}'", expectedRouteName);

        }

This is the Model:

public class Company
{

[Key]
public int CompanyId { get; set; }

[Required(ErrorMessage = "* (xxxx)")]
[MaxLength(7)]
[RegularExpression("^([A-Z0-9]{7})$", ErrorMessage = "xxx")]
[Display(Name = "Code")]
public string Code { get; set; }

[Required(ErrorMessage = "*")]
[MaxLength(40)]
[Display(Name = "Name")]
public string Name { get; set; }

}

When I Run the test, this function always return false: var result = _commandBus.Submit(command); and the test fails.

I don’t know how to test it? Should I mock the _commandBus and set it to return true? I tried in this way but unsuccessfully:

        var mockCommand = Substitute.For<ICommand>();

        mockProcessor.Submit(mockCommand).Success.Returns(true);

To create this project I have got inspiration from this http://efmvc.codeplex.com/

Any advise to me? Thanks.

  • 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-06-04T06:28:51+00:00Added an answer on June 4, 2026 at 6:28 am

    The command you pass to mockProcessor.Submit(mockCommand).Success.Returns(true) needs to be the same one that the code-under-test passes. As the method news up its own command this will never be the case.

    Easiest fix is to match any command when you set up your substitute:

    var result = CreateSuccessfulResult(); // <-- fill this in as appropriate
    mockProcessor.Submit(null).ReturnsForAnyArgs(result);
    

    Setting the Success field on result inline as per your original test should work too I think, but I find it a bit clearer to specify the result required.

    You can improve this a bit by matching the expected command type:

    mockProcessor.Submit(Arg.Any<CreateOrUpdateCompanyCommand>()).Returns(result);
    

    You can also inspect the command passed if you’d like to test that:

    mockProcessor
        .Submit(Arg.Is<CreateOrUpdateCompanyCommand>(x => x.CompanyId = ...))
        .Returns(result);
    

    A similar approach can be used to check the Validate call.

    There’s a bit more info in the NSubstitute docs.

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

Sidebar

Related Questions

i have this controller: public ActionResult Novo() { var products = context.Product.Select(x => new
this is my controller: public ActionResult Create() { Number newNumber = new Number(); return
this is my controller: public ActionResult Create() { Number newNumber = new Number(); return
I have the following code in my controller: // GET: /Review/Create public ActionResult Create()
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
I have this command in a Rails controller open(source) { |s| content = s.read
I have this controller in Code Igniter application. A value is initialized in the
I have this controller in Code Igniter that begins with class MyController extends CI_Controller
I have this in my controller. I need my view to generate some HTML,
I have this in my controller: MyTestViewModel asdf = new MyTestViewModel { SomeTestDate =

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.