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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:27:21+00:00 2026-06-11T10:27:21+00:00

I am using Sharp Architechture and Rhino Mocks with NUnit. I have a test

  • 0

I am using Sharp Architechture and Rhino Mocks with NUnit.

I have a test service that looks like this

public class TestService : ITestService {
    public TestService(ITestQueries testQueries, IRepository<Test> testRepository,
                       IApplicationCachedListService applicationCachedListService) {
        Check.Require(testQueries != null, "testQueries may not be null");
        Check.Require(applicationCachedListService != null, "applicationCachedListService may not be null");
        _testQueries = testQueries;
        _testRepository = testRepository;
        _applicationCachedListService = applicationCachedListService;
    }

I then have this method in my service

public string Create(TestFormViewModel viewModel, ViewDataDictionary viewData, TempDataDictionary tempData) {
        if (!viewData.ModelState.IsValid) {
            tempData.SafeAdd(viewModel);
            return "Create";
        }

        try {
            var test = new Test();
            UpdateFromViewModel(test, viewModel);
            _testRepository.SaveOrUpdate(test);
            tempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()]
                = string.Format("Successfully created product '{0}'", test.TestName);
        }
        catch (Exception ex) {
            _testRepository.DbContext.RollbackTransaction();
            tempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()]
                = string.Format("An error occurred creating the product: {0}", ex.Message);
            return "Create";
        }

        return "Index";


    }

}

I then have a Controller that looks like this:

[ValidateAntiForgeryToken]
    [Transaction]
    [AcceptVerbs(HttpVerbs.Post)]
    [ModelStateToTempData]
    public ActionResult Create(TestFormViewModel viewModel) {
        return RedirectToAction(_testService.Create(viewModel, ViewData, TempData));
    }

I want to write a simple test to see if when !viewData.ModelState.IsValid I return “Create”.

I have this so far but am confused because it really is not testing the controller it is just doing what I am telling it to do in the return.

[Test]
    public void CreateResult_RedirectsToActionCreate_WhenModelStateIsInvalid(){
        // Arrange
        var viewModel = new TestFormViewModel();
        _controller.ViewData.ModelState.Clear();
        _controller.ModelState.AddModelError("Name", "Please enter a name");


        _testService.Stub(a => a.Create(viewModel, new ViewDataDictionary(), new TempDataDictionary())).IgnoreArguments().Return("Create");

        // Act
        var result = _controller.Create(viewModel);

        // Assert            
        result.AssertActionRedirect().ToAction("Create"); //this is really not testing the controller??.



    }

Any help is appreciated.

  • 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-11T10:27:22+00:00Added an answer on June 11, 2026 at 10:27 am

    It looks like you try to write not Unit tests. It’s more like Integration test. Following Unit-testing ideology you have two Units: Service and Controller. The idea is that you should test each unit separately and keep your tests simple. According this first of all you should write tests for TestService. After that, when you cover it, write tests for your Controller using Stubs/Mocks for TestService. So your test for Controller looks right, it tests that redirect is happened according to result from Service.Create method. You should add tests for your TestService without controller context and it’s got a good coverage.
    If you want to test this units together then you must not use mocks and it will be more like Integration tests.
    Additionally to cover integration between modules you can write web based tests using some tools like WatiN or Selenium for testing whole application.
    But in any case write unit tests for separate parts is a good practice.

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

Sidebar

Related Questions

Using Sharp Architecture 1.9 I have a base class that inherits from the Sharp
i'm using Asp.net MVC with Sharp Architecture. I have this code: return _repositoryKeyWord.FindAll(x =>
Morning guys. Using C sharp .net4, and MS Visual Studio 2010. I have Developed
I am using Wcf in Sharp Architecture. I have configured my project following northwind
I have a Sharp Architecture based app using Fluent NHibernate with Automapping. I have
I have been reading about using Sharp Architecture to control nhiberate via Wcf. Is
I'm using Bing Sharp 2.0. I'd like to do image search by filters, so
I have a set of Win32 applications that share information using a shared memory
I am currently using PDF Sharp in .NET to generate PDF documents which heavily
I'm using GTK Sharp to work on some GUI for my app. Take a

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.