How do I check “no exception occurred” in my MSTest unit test?

后端 未结 6 1995
再見小時候
再見小時候 2020-12-30 18:34

I\'m writing a unit test for this one method which returns \"void\". I would like to have one case that the test passes when there is no exception thrown. How do I write t

6条回答
  •  失恋的感觉
    2020-12-30 18:38

    I like to see an Assert.Whatever at the end of each test, just for consistency... without one, can I really be sure there's not supposed to be one there?

    For me, this is as simple as putting Assert.IsTrue(true);

    I know I didn't accidentally put that code in there, and thus I should be confident enough at quick a skim through that this was as intended.

        [TestMethod]
        public void ProjectRejectsGappedVersioningByDefault() {
    
            var files = new List();
            files.Add(ScriptProjectTestMocks.GetVersion1to2());
            files.Add(ScriptProjectTestMocks.GetVersion3to4());
    
            Assert.Throws(() => {
                var sut = new ScriptProject(files);
            });
    
        }
    
        [TestMethod]
        public void ProjectAcceptsGappedVersionsExplicitly() {
    
            var files = new List();
            files.Add(ScriptProjectTestMocks.GetVersion1to2());
            files.Add(ScriptProjectTestMocks.GetVersion3to4());
    
            var sut = new ScriptProject(files, true);
    
            Assert.IsTrue(true);   // Assert.Pass() would be nicer... build it in if you like
    
        }
    

提交回复
热议问题