How to simulate network failure for test purposes (in C#)?

前端 未结 11 822
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 04:25

I\'m building what could be called the DAL for a new app. Unfortunately, network connectivity to the database is a real problem.

I\'d like to be able to temporarily

11条回答
  •  长情又很酷
    2020-12-29 05:04

    If you are trying a complete network outage for your application unplugging the network cable will work. Sometimes you might have a data access layer with multiple data sources (on different machines) in which case you can simulate an exception in your tests with a Mock Framework like Rhino Mocks. Here is some pseudo-code that you may have in your test

    void TestUserDBFailure()
    {
        // ***** THIS IS PSEUDO-CODE *******
        //setting up the stage - retrieval of the user info create an exception
        Expect.Call(_userRepository.GetUser(null))
           .IgnoreArguments()
           .Return(new Exception());
    
        // Call that uses the getuser function, see how it reacts
        User selectedUser = _dataLoader.GetUserData("testuser", "password");        
    }
    

提交回复
热议问题