0

I am struggling to work out how to unit test a method like the below:

    public bool EmailSomething(OrderType order)
    {
        var subject = "Email Subject";

        var body = File.ReadAllText("/EmailTemplates/Email.html");

        body = body.Replace("#orderref#", order.OrderID.ToString());
        if (SendEmail(new MailAddress("email@email.com"), subject, body))
        {
            return true;
        }
        return false;
    }

The bit I am struggling with is the File.ReadAllText(); is returning an exception in a unit test since I am not referencing it correctly (for the unit test).

How would I unit test this method?

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • This isn't really unit testing as it stands, as it's testing the `ReadAllText` method as well as `MailAddress`, `String.Replace` and `SendEmail`. – OMGtechy Apr 07 '15 at 20:29
  • ok, lets call it an integration test. I want to test that my code is working correctly. I do not want to test the functionality of the .NET framework. – Jamie Rees Apr 07 '15 at 20:31
  • This code isn't easily testable. You would have to rewrite it with testing in mind. That's the point of TDD. – Nick Apr 07 '15 at 20:34
  • This search http://www.bing.com/search?q=c%23+unittest+file+io may give you more information to improve your question if you don't like duplicate. – Alexei Levenkov Apr 07 '15 at 20:35
  • 1
    Just to get you on the right track, instead of directly using IO in this method an object or service of some kind should be injected as an instance member of whatever class this is in or passed as a parameter. The injected object or service can be mocked out in testing to simulate IO. – Nick Apr 07 '15 at 20:39
  • 1
    Helpful link with suggestions on mocking static method calls: http://stackoverflow.com/questions/12580015/how-to-mock-static-methods-in-c-sharp-using-moq-framework. – dugas Apr 07 '15 at 20:40

0 Answers0