Moths / fakes: how do I do a test setup?

I recently worked with Moles, and now I'm moving on to Fakes. In my old test project, I had a test setup that looked like this:

[TestInitialize] public void Setup() { //... } 

There I made some necessary adjustments, as well as setting up some of my objects.

The method of testing in moles looked somehow (with also [HostType ("Moles")], indicating that it uses moles objects.

 [TestMethod] [HostType("Moles")] public void MolesTestMethod() { //... } 

Now, in fakes, they no longer use the HostType attribute. Instead, they use a ShimsContext in which you can use your "mocked" classes. It looks something like this:

 [TestMethod] public void FakesTestMethod() { using (ShimsContext.Create()) { //... } } 

If you do not use this context, you may receive an error message. This basically suggests that there was a ShimInvalidOperationException in FakesTestMethod, and you should use ShimsContext.Create () as follows)

 -- C#: using Microsoft.QualityTools.Testing.Fakes; using(ShimsContext.Create()) { // your test code using Shims here } -- VisualBasic.NET Imports Microsoft.QualityTools.Testing.Fakes Using ShimsContext.Create ' your test code using Shims here End Using 

So, I tried to put my setup calls in this context and got something like this:

 [TestInitialize] public void Setup() { using(ShimsContext.Create()) { //... } } 

Now, if I use this context in my installation method, the entire installation that runs there will end after the context and will not be valid anymore when the unit tests actually run, which is actually not the case. I want to use the test installation method.

I fixed this problem by placing it inside the testing method itself and simply calling the private installation method right in this context and before the test code. This configuration method now does all the processing that was before [TestInitialize]. The code looks something like this:

 [TestMethod] public void PerformActionFromConfigActionStateActionIdIsSet() { using (ShimsContext.Create()) { Setup(); //... } } 

My problem with this problem is that this solution completely kills the idea of ​​the [TestInitialize] installation method. I have to duplicate this code in EVERY test method and the most important part: objects created in this Setup () method will be created and destroyed for the EACH test, which is not at all perfect!

Is there any other way to set test data in fakes? Any help is appreciated!

+6
source share
2 answers

Using:

Defines the area beyond which the object or objects will be deleted.

You create an instance of IDisposable by calling ShimsContext.Create() and wrapping it with a usage block. After initializing your Fakes classes and leaving the scope of use, your configuration will be deleted.

I would recommend creating an instance of IDisposable and calling Dispose at the end of the tests manually.

If you want to avoid creating a Context for each test, I would also recommend using ClassInitialize and ClassCleanup instead of TestInitialize and TestCleanup, as this should be sufficient to initialize Shims once for all tests. This is only possible if there are no additional dependencies (see Oleg Sykh's answer).

 [TestClass] public class TestClass1 { protected static IDisposable Context { get; set; } [ClassInitialize] public static void ClassInitialize(TestContext testContext) { // Create ShimsContext Context = ShimsContext.Create(); // TODO: Additional setup } [ClassCleanup] public static void ClassCleanup() { Context.Dispose(); Context = null; } [TestMethod] public void TestMethod1() { // Fakes should be initialized correctly here } [TestMethod] public void TestMethod2() { // Fakes should be initialized correctly here } } 

Hope this helps.

+12
source

Using ClassInitialize / ClassCleanup to initialize pads is not a good idea. This will make the locks configured for one test method remain active for all other test methods in the class. In other words, workarounds and any additional state captured by lambdas are shared between all test methods. It can also destabilize the test harness if you bypass the methods that it uses.

Instead, use TestInitialize / TestCleanup to create / remove the ShimsContext for each test method separately.

+4
source

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


All Articles