CSLA 6 integration tests #2641
Replies: 2 comments 5 replies
-
I actually don't know: does mstest use DI to create the test objects? If so, you could inject the required services into the test class. I imagine not though. What you need is access to the My guess is that the entire test suite runs within one DI scope - as created in the Basically, what you need to do is implement a global static property - probably in whatever class hosts your public class TestHost
{
public void AssemblyInitialize()
{
var server = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseConfiguration(new ConfigurationBuilder()
.AddJsonFile("appsettings.json") //the file is set to be copied to the output directory if newer
.Build()
.ConfigureCsla()
.AddCsla())
.UseStartup<Startup>());
ApplicationContext = server.Services.GetRequiredService<ApplicationContext>();
}
public static ApplicationContext ApplicationContext { get; private set; }
} Then, throughout your test code, when you need an instance of a service, like [TestMethod]
public void CustomerSearchAll_CustomerSearchList_Count()
{
var criteria = CustomerSearchCriteriaEdit.CreateCriteria();
var dp = TestHost.ApplicationContext.CreateInstanceDI<DataPortal<CustomerSearchList>>();
var searchList = dp.Fetch(criteria);
Assert.AreEqual(5, searchList.Count, "Expected Customer Search count = {0}", 5);
} |
Beta Was this translation helpful? Give feedback.
-
I know this is now an old post and that the O/P probably considers it resolved, but I've only just seen it. I've been working on the tests for Csla itself, so I have some experience in this area and it felt like posting my thoughts on this subject might be useful to others who follow our path. Unless you need it for something else, you don't need to (and I wouldn't) make use of WebHostBuilder in tests. Fortunately Rocky showed me the path in that regard when it comes to using DI in tests; you only need to create an instance of the ServiceCollection type, configure it, and then build it. Here's a snippet from some working tests in the Csla source itself: // Initialise DI
var services = new ServiceCollection();
// Add Csla
services.AddSingleton<Csla.Server.Dashboard.IDashboard, Csla.Server.Dashboard.Dashboard>();
services.AddCsla();
serviceProvider = services.BuildServiceProvider();
// This instance of IServiceProvider can now be used to create instances of things, for example:
var dataPortal = serviceProvider.GetRequiredService<IDataPortal<MyBusinessType>>(); This code will probably evolve as the updated configuration methods in Csla 6 are finalized, so don't take this snapshot of the code as it was the day I posted it as final. I'm putting this functionality into an assembly called Csla.TestHelpers for use in performing the tests that check the framework itself, so that's the best place to look for more up to date examples of how to do this as future you has the same problem as me here in the past! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am starting to convert existing CSLA 5.5 objects to CSLA 6 and are running into an issue with creating the same integration tests against a mock repository.
For CSLA 5.5 I have an AssemblyInitialize mehtod in the test project that will create the required services e.g.
My objects use static methods to call the dataportal ( which is not allowed anymore).
For the tests, I will call these methods to create the objects e.g.
What needs to change to get this working in CSLA 6?
I understand that I will probably use factory objects to inject the dataportal to create the csla objects, but I have no idea how to configure/call this in an integration test.
Beta Was this translation helpful? Give feedback.
All reactions