1

I have a class and method viz. -

public class Class1
{
  public void SomeMethod(int x, int y)
  {
     using(var scope = new TransactionScope())
     {
       if(x == 0 && y == 0)
       {
          //Do Something
       }
       if(x ==1)
       {
         //Do something
       }

       // Need to throw an exception for running a test case to check   
       // rollback scenario            

       if(y == 2)
       {
          //Do something
       }
       scope.Complete();
     }
  }
}

I want to test the transaction scope rollback stuff and so I want to inject an exception in between the above code (i.e. want to replace the " // Need to throw an exception for running a test case..." part with throw new Exception("some message")) and run the in memory re-complied method to test if something goes wrong in between the code execution then there will be roll back.I don't want to change anything in my existing code just for testing the rollback scenario.

Whatever I have read about Roslyn(I am very new to this .NET space), I think it can be done using some Roslyn API. But I am not sure exactly how to do it since I am not much acquainted with the same.If any one can give some sample based suggestion, that would be really helpful.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
sandip ray
  • 57
  • 1
  • 8
  • 1
    I would inject a `Do something` that throws. But I woudl use normal DI practices. – Paulo Morgado Jan 20 '16 at 09:23
  • Byt the way, "Roslyn" is the new compiler platform for .NET (Visual C# And Visual Basic) and only works on source code text. There's no way to re-compile. It just compiles! – Paulo Morgado Jan 20 '16 at 09:24
  • @PauloMorgado: It seemed like an overhead to me to inject something (may be some Action method object) just for one single testing purpose and also would need to invoke the action within the code and this would be of no use actually in production code. If I provide the relative file path to Roslyn API, is it possible to serve the above mentioned purpose? I am ready to have a new file all together which should be fine following the Open Closed principle. – sandip ray Jan 20 '16 at 09:59
  • DI can be as simple as just a delegate but can be deeper. And it benefits testability, which is what you are looking for. Nevertheless, to point you in the direction you want, Jash Varty has a series of blog posts on [his blog](https://joshvarty.wordpress.com/) about using "Roslyn". – Paulo Morgado Jan 20 '16 at 10:17
  • @PauloMorgado: Might be possible to get the source code from the type using [Mono.Cecil](http://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/) as indicadted [here](http://stackoverflow.com/questions/2693881/c-sharp-can-i-use-reflection-to-inspect-the-code-in-a-method) by Aaronaught and Ark-kun. – sandip ray Jan 20 '16 at 10:18
  • @PauloMorgado : Thanks for pointing to Josh Varty's blog. I just want to keep the actual code cleaner and separated from any "ONLY test related" dependencies and that's why trying to go via the "a bit" hard route. – sandip ray Jan 20 '16 at 10:22
  • 1
    @sandipray If you're concerned about impacting the production code then put that code a precompiler check so that it's only compiled in when testing. I imagine that what you're trying to do is going to be hard enough that the odds of you messing up how you test this is greater than you messing up the actual method being tested. If the probability of error in testing is higher than the probability of error in the thing being tested then you're not really getting value out of your tests. – Servy Jan 20 '16 at 15:13
  • @Servy : Very valid point.Will implement it as per your suggestion.Thanks a lot. – sandip ray Jan 21 '16 at 06:16
  • Can't you mock the `Do something` bit to throw an exception and inject it using DI? {edit} Sorry, that's exactly what @PauloMorgado said in the first place, missed that. – CompuChip Jan 21 '16 at 12:24

1 Answers1

-1

You can definitely do this with Roslyn. It's going to be the same tooling as writing an AOP compiler. You'll need to modify the code and then compile it. And additionally you'll want to run the test on the assemblies of the modified code. It's not straight forward right now how to do this with Roslyn, but you can try the approach described here.

Community
  • 1
  • 1
Tamas
  • 6,260
  • 19
  • 30