Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a global variable in unit testing?

I need an object that was initialized in the first test in other tests too. To prevent code duplication, I want to save the object I created in the first test in a global variable. This is what I did:

namespace Project.WebUIV2.Tests {
    [TestClass]
    public class FilmSelectieControllerTests {
        private FilmSelectieController controller;
        [TestMethod]
        public void CanInitializeController() {
            Mock<IRepository<Voorstelling>> mockIRepository1 = new Mock<IRepository<Voorstelling>>();
            Mock<IRepository<Film>> mockIRepository2 = new Mock<IRepository<Film>>();
            Mock<IRepository<Zaal>> mockIRepository3 = new Mock<IRepository<Zaal>>();
            Film film = new Film { titel = "Film" };
            mockIRepository2.Setup(m => m.Add(film));
            var repository1 = mockIRepository1.Object;
            var repository2 = mockIRepository2.Object;
            var repository3 = mockIRepository3.Object;
            var controller = new FilmSelectieController(repository1, repository2, repository3);
            Assert.IsNotNull(controller);
            Assert.IsInstanceOfType(controller, typeof(FilmSelectieController));
            this.controller = controller;

        }

        [TestMethod]
        public void IsCreated() {
            Assert.IsInstanceOfType(controller, typeof(FilmSelectieController));
        }
    }
}

Testmethod CanInitializeController() succeeded, while isCreated() failed.

In the first test i created the FilmSelectieController object.

How do I use that object in next tests?

Thanks to Anthony Pegram

Solution:

..*/
private Controller controller;

[TestInitialize]
public void Initialize() {
    var parameter = /*...test data that i need in all tests...*/
    this.controller = new Controller(parameter)
}

[TestMethod]
public void test1 {
    result = controller.FirstMethod();
    //assert something
}

[TestMethod]
public void test2 {
    result = controller.SecondMethod();
    //assert something
}
/*..
like image 382
Janneman96 Avatar asked Mar 18 '15 16:03

Janneman96


1 Answers

Create a method to run on initialization and setup any object that needs to be available for all tests within that method. (The method will rerun before each test, so do not expect state to persist.)

[TestInitialize]
public void Initialize()
{
     // your common setup code here
     this.controller = ...
}

(TestInitialize is the attribute under MS Test, a similar attribute will be available under most other testing frameworks you might use.)

I typically do this for stubs and mocks that I will be using in the test. As far as setting up the class I'm actually testing, I'll typically handle that in the test method itself or refactor it out to a private helper method, but that's just a personal preference. If the setup of the class under test is also uniform for all your tests, then it can also go in the initialization method.

like image 190
Anthony Pegram Avatar answered Sep 18 '22 22:09

Anthony Pegram