Flagsmith / flagsmith-dotnet-client

.NET Standard Client for Flagsmith. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://www.flagsmith.com/
https://www.flagsmith.com/
BSD 3-Clause "New" or "Revised" License
19 stars 12 forks source link

Add interfaces to allow unit testing in .NET app using the FlagsmithClient #57

Closed JFCote closed 1 year ago

JFCote commented 1 year ago

Most of .NET mocking framework like Moq only allow to mock interfaces.

dabeeeenster commented 1 year ago

Thanks! Will check it out

dabeeeenster commented 1 year ago

Are we able to get the tests green @JFCote ?

JFCote commented 1 year ago

@dabeeeenster Will check this today! I don't really understand why adding interfaces would break the test but will have a look :)

JFCote commented 1 year ago

@dabeeeenster Do you have any documentation on how to run the unit tests on a local environment? I have cloned the code and I'm trying dotnet test in the root folder but I receive this weird error which is not even the same error I can see in the PR:

PS C:\dev\flagsmith-dotnet-client> dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  Flagsmith.Engine -> C:\dev\flagsmith-dotnet-client\Flagsmith.Engine\bin\Debug\netstandard2.0\Flagsmith.Engine.dll
  Flagsmith.FlagsmithClient -> C:\dev\flagsmith-dotnet-client\Flagsmith.FlagsmithClient\bin\Debug\netstandard2.0\Flagsmith.FlagsmithClient.dll
  EngineTest -> C:\dev\flagsmith-dotnet-client\Flagsmith.EngineTest\bin\Debug\net6.0\EngineTest.dll
Test run for C:\dev\flagsmith-dotnet-client\Flagsmith.EngineTest\bin\Debug\net6.0\EngineTest.dll (.NETCoreApp,Version=v6.0)
  ClientTest -> C:\dev\flagsmith-dotnet-client\Flagsmith.Client.Test\bin\Debug\net6.0\ClientTest.dll
Test run for C:\dev\flagsmith-dotnet-client\Flagsmith.Client.Test\bin\Debug\net6.0\ClientTest.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.5.0 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Microsoft (R) Test Execution Command Line Tool Version 17.5.0 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.79]     EngineTest.EngineTest.Test_Engine [FAIL]
  Failed EngineTest.EngineTest.Test_Engine [1 ms]
  Error Message:
   System.IO.DirectoryNotFoundException : Could not find a part of the path 'C:\dev\flagsmith-dotnet-client\Flagsmith.EngineTest\EngineTestData\data\environment_n9fbf9h3v4fFgH3U3ngWhb.json'.
  Stack Trace:
     at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path)
   at EngineTest.EngineTest.LoadData(String path) in C:\dev\flagsmith-dotnet-client\Flagsmith.EngineTest\EngineTest.cs:line 52
   at EngineTest.EngineTest.ExtractTestCases(String path) in C:\dev\flagsmith-dotnet-client\Flagsmith.EngineTest\EngineTest.cs:line 60

Failed!  - Failed:     1, Passed:   174, Skipped:     0, Total:   175, Duration: 649 ms - EngineTest.dll (net6.0)

Passed!  - Failed:     0, Passed:    24, Skipped:     0, Total:    24, Duration: 12 s - ClientTest.dll (net6.0)
dabeeeenster commented 1 year ago

Can you try running the tests a few times? We think there may be a thread/race condition with the testing harness?

JFCote commented 1 year ago

@dabeeeenster I can run it as much time as I want on my local machine, and I always receive the same error (see my previous comment). I tried both in bash and in powershell. Also, I have the same error on the main branch that I receive on my branch, so I don't think I broke anything.

Any other things you want me to try?

matthewelwell commented 1 year ago

Hi @JFCote, I'll take a look into this further and get back to you.

JFCote commented 1 year ago

@matthewelwell Thanks! We can hop on Gitter or something like that if you want and work it out. Let me know.

dabeeeenster commented 1 year ago

@JFCote we have a discord https://discord.gg/hFhxNtXzgm