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()) {
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!