I inject dependency in test by anther trick
[TestFixture]
public class SshToolClientTests
{
private ServiceProvider serviceProvider { get; set; }
private ISshToolClient client=> serviceProvider.GetService<ISshToolClient>();
public SshToolClientTests() {
serviceProvider = new ServiceCollection()
.AddTransient<ISshToolClient, SshToolClient.SshToolClient>()
.BuildServiceProvider();
}
[OneTimeTearDown]
public void OneTimeTeardown()
{
serviceProvider.Dispose();
}
[SetUp]
public void Setup()
{
}
[Test]
public void GenerateConfigFile( )
{
ISshConfig config= client.GetDefaultConfiguration();
Assert.IsNotNull(config);
var json=JsonConvert.SerializeObject(config, Formatting.Indented);
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "generatedSshConfig.json");
File.WriteAllText(path, json);
TestContext.WriteLine(@$"file:///{path.Replace('\\','/')}");
}
}
Александр Шмыков
Net dev
Something wrong My test marked as attribute [NUnit.Extension.DependencyInjection.DependencyInjectingTestFixture] can not run . Building Test Projects ========== Starting test run ========== ========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ========== Building Test Projects Executing all tests in file: SshToolClientTests.cs ========== Starting test run ========== ========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========