nivolas
nivolas

Reputation: 317

With Nunit, how to test if a method has caught an exception?

I have a method to save data in my database (C# and Mysql). I'm using the MVP model, so what it does is Save in Presenter > Save in Service > Save in manager > Save in dao. I do the data integrity control in the manager.

I want to test my presenter. The first save test is done with incorrect values to check if my manager throws an exception. The problem is that my presenter catch this exception, so I can't use the Assert.Throws<Exception>(() => {DoSave();}) because the exception is not seen by the Assert. Is there an Assert in NUnit to check if an exception has been raised within the tested code without reaching the test itself?

Simplified, it looks like that : (Presenter)

    private void SaveData()
    {
            try
            {
                //Calls service who calls manager who checks data then call DAO to save.
                if (MyService.SaveData(currentData, ReferentielType.ENEG))
                {
                    //Success
                }
                else
                {
                    //Failure
                }
            }
            //Thrown by the manager if the data are incorrect
            catch (DataInvalidException ex)
            {
                // log exception as error
                TraceHelper.TraceError(ex, "The data {0} is not correct", currentData.Code);

                // send a message to user
                MessageContainer.Instance.SendErrorMessage("DataInvalid", BusinessError.DataInvalid, currentData.Code);
            }
        }
    }

And my test :

[Test]
    public void UpdateDataTest()
    {
        //Test setup 
        //making fake data
        //When the var etatDuTest is different from 0, it means some of the data are invalid

        //Instance of the presenter
        MockView view = new MockView();
        PresenterToTest presenter = new PresenterToTest(view);

        //Testing with invalid values
        //bad label
        ((MockView)((IView)presenter.View)).etatDuTest = 1;
        Assert.Throws<DataInvalidException>(() => { ((MockView)((IView)presenter.View)).RaiseSaveDataClicked(); });
        //Bad num eva
        ((MockView)((IView)presenter.View)).etatDuTest = 2;
        Assert.Throws<DataInvalidException>(() => { ((MockView)((IView)presenter.View)).RaiseSaveDataClicked(); });

        //Testing with everything OK
        //Tout est bon logiquement.
        ((MockView)((IView)presenter.View)).etatDuTest = 0;
        ((MockView)((IView)presenter.View)).RaiseSaveDataClicked();

    }

Upvotes: 0

Views: 1904

Answers (1)

Callum Bradbury
Callum Bradbury

Reputation: 956

Testing that an exception has been caught is by itself a pointless test. You should be testing what happens within the catch block, that will verify that the exception was caught.

If you can verify that your MessageContainer.Instance has that DataInvalid error message, that's your best option really.

Upvotes: 1

Related Questions