locked
xUnit / nUnit testing fails on Mac RRS feed

  • Question

  • User394207 posted

    Hello everyone,

    I'm trying to unit test my app and I have encountered a strange issue. I've written a simple unit test (jsut testing an add function for now) just for the purpose to test the frameworks and and see if it performs well on both Windows MacOS.

    Now I've put the add function in one of my exsiting classes which uses an Object from this BLE Plugin. Running it in Windows works flawless with both frameworks. But as soon as I run it on MacOS both frameworks are failing to run the test with teh following error (Yes, I do have a refrence to my main project. As already mentioned it works fine on Windows.) :

    ``` System.TypeLoadException : Could not load type of field 'MyApp.Models.MyAppClass:myClassSensor' (0) due to: Could not load file or assembly 'Plugin.BLE.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

    Stack trace:
      at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
      at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <55adae4546cd485ba70e2948332ebe8c>:0 
    

    ```

    Here is the CUT:

    ``` using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using Plugin.BLE; using Plugin.BLE.Abstractions.Contracts; using Plugin.BLE.Abstractions.Exceptions;

    namespace MyApp.Models { class MyAppClass { private IDevice myClassSensor;

        public MyAppClass()
        {
        }
    
        public IDevice GetSensor() => myClassSensor;
    
        internal int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    } ```

    And here the test I'm using:

    ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyApp.Models; using Xunit;

    namespace MyApp.Tests.ModelsTests { public class MyAppClassTest { [Fact] public void AddTest() { var obj = new MyAppClass();

            var val = obj .Add(3, 1);
    
            Assert.Equal(4, val);
        }
    }
    

    } ```

    I hope someone can help me out.

    Best Regards BaseCrusher

    Monday, May 25, 2020 9:28 AM

All replies

  • User74 posted

    If you want to unit test your model you should mock its dependencies.

    How is myClassSensor set? It should be set with a mocking framework as part of the setup of the test. Otherwise when you have a real test that references myClassSensor it will the BLE plugin code, and you don't want to do that in your test. The test should test your view model in isolation.

    Monday, May 25, 2020 9:55 AM
  • User394207 posted

    I mean yes, dependencies should be mocked but I don't use them in this specific test and as mentioned above on Windows the exact same code works flawless. The test ist passed without any issues. But I'll give it a try and report again.

    To clarify how myClassSensor is set. It's being set after initialization(It could go into the constructor but I left it out until I'm more comfortable using dependency injection).

    I left the code that is not being tested out of "MyClass", so its easier to understand. But the test case only tests the add method. Thats why im so confused about this test not passing on MacOS.

    Monday, May 25, 2020 11:22 AM
  • User394207 posted

    Hmm, thats odd.

    In order to mock IDevice I need to add the BLE NuGet. But after adding it, atleast on MacOS the test crashes. resulting in me not even being able to mock it. :/

    Any ideas?

    Monday, May 25, 2020 12:52 PM
  • User74 posted

    You could create your own class and interface and abstract all the BLÉ calls into that. Then you don’t need to référence BLÉ directly in your VM.

    Monday, May 25, 2020 8:36 PM
  • User394207 posted

    @JohnH said: You could create your own class and interface and abstract all the BLÉ calls into that. Then you don’t need to référence BLÉ directly in your VM.

    Eventhough this would be a good workaround for my problem and I believe as of now there is no other way to solve this. I would still like to know why the refrence doesn't work on MacOS but it does on Windows.

    I mean its just a litle tedious to reimplement every interface that already implemented by the BLE framework. And while it isn't much work with this framework, encountering this issue with other much larger frameworks could result in hours of extra work.

    Tuesday, May 26, 2020 11:47 AM