Why does this asp.net mvc unit test fail?

I edited and simplified this question a lot.

If I have this method on my HomeController:

    public ActionResult Strangeness( int id )
    {
        StrangenessClass strangeness = null;

        if( id == 1 )
        {
            strangeness = new StrangenessClass() { Name="Strangeness", Desc="Really weird behavior" };
        }

        return View( strangeness );
    }

And this class:

public class StrangenessClass
{
    public string Name { get; set; }
    public string Desc { get; set; }
}

Why does this unit test not work?

    [TestMethod]
    public void Strangeness()
    {
        HomeController controller = new HomeController();

        ViewResult result = controller.Strangeness( 1 ) as ViewResult;
        var model = result.ViewData.Model;
        result = controller.Strangeness( 2 ) as ViewResult;
        model = result.ViewData.Model;

        Assert.IsNull( model );
    }

I understand that, as a rule, I will have one test to check the zero condition, and the other to check the good condition, but I ran into this problem when testing my deletion controller. In a delete test, I usually take a record, delete the record, and then try to extract it again. It should be zero the second time I get it, but it is not. So, I boiled the problem as described above.

If this is not the right way to test deletions, how would you do it? You do not need to make sure that the record is really deleted?

0
2

, , .

, MVC, :

protected internal virtual ViewResult View(string viewName, string masterName, object model)
{
    if (model != null)
    {
        base.ViewData.Model = model;
    }
    return new ViewResult { ViewName = viewName, MasterName = masterName, ViewData = base.ViewData, TempData = base.TempData };
}

null, ViewData.Model. , HomeController.Strangeness.

+1

, . "" "", "" . ? , Delete Arrange?

_stateService? unit/integration?

, , , , . , , .

, _stateService - , , , ( Rhino Mocks MVCContrib.TestHelper):

[TestClass]
public class DevisControllerTests : TestControllerBuilder
{
    private HomeController _sut; // Subject Under Test
    private IStateService _stateServiceStub; // Dependency of the SUT

    [TestInitialize()]
    public void MyTestInitialize()
    {
        _stateServiceStub = MockRepository.GenerateStub<IStateService>();
        _sut = new HomeController(_stateServiceStub);
        InitializeController(_sut); // this method comes from the base class TestControllerBuilder
    }

    [TestMethod]
    public void HomeController_Delete_Action_Should_Fetch_State_From_Db_And_Pass_It_To_The_View()
    {
        // arrange
        var id = 4;
        var expectedState = new State();
        _stateServiceStub.Stub(x => x.GetById(id)).Return(expectedState);

        // act
        var actual = _sut.Delete(id);

        // assert
        actual
            .AssertViewRendered()
            .WithViewData<State>()
            .ShouldBe(expectedState);
    }

    [TestMethod]
    public void HomeController_Delete_Action_Handler_Should_Return_Default_View_If_Model_Null()
    {
        // act
        var actual = _sut.Delete(null);

        // assert
        actual.AssertViewRendered();
    }

    [TestMethod]
    public void HomeController_Delete_Action_Handler_Should_Return_View_If_Exception_Thrown_From_Service()
    {
        // arrange
        var model = new State();
        _stateServiceStub.Stub(x => x.Delete(model)).Throw(new Exception("oops"));

        // act
        var actual = _sut.Delete(state);

        // assert
        actual
            .AssertViewRendered()
            .WithViewData<State>()
            .ShouldBe(model);
    }


    [TestMethod]
    public void HomeController_Delete_Action_Handler_Should_Redirect_If_Model_Successfully_Deleted()
    {
        // arrange
        var model = new State();

        // act
        var actual = _sut.Delete(state);

        // assert
        actual
            .AssertActionRedirect()
            .ToAction<HomeController>(c => c.Index());

        _stateServiceStub.AssertWasCalled(x => x.Delete(model));
    }

}
0

Source: https://habr.com/ru/post/1748592/


All Articles