0

I am trying to get to grips with mocking and ReSharper by watching this demo http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq I'm sure I followed the code correctly but I'm getting errors. Could you give me some pointers as to were I'm going wrong?

errors:-class name is not valid at this point errors:-send email is not implemented

using System;
using System.Collections.Generic; // for the Dictionary
using NUnit.Framework;
using Moq;


namespace MvcUnitTst2.Tests
{
[TestFixture]
public class IntoToMoq
{
    [Test]
    public void NonMock()
    {

        var emailService = new EmailService();
        var emailer = new Emailer(emailService);
        emailer.SendBatchEmails();

    }
}

public class Emailer
{
    public Emailer(IEmailService emailService)
    {
        // error here:- class name is not valid at this point
        MvcUnitTst2.Tests.EmailService = emailService;
    }
    public void SendBatchEmails()
    {
        // build a list of emails
        // code below fails
        //Dictionary<string, string> emails = new Dictionary<string, string>
        //                                        {
        //                                            {"fred1@foo.com","Hello 1"},
        //                                            {"fred2@foo.com","Hello 2"},
        //                                            {"fred3@foo.com","Hello 3"},
        //                                            {"fred4@foo.com","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"fred1@foo.com","Hello 1"},
                                                    {"fred2@foo.com","Hello 2"},
                                                    {"fred3@foo.com","Hello 3"},
                                                    {"fred4@foo.com","Hello 4"}
                                                };                                       



        foreach (KeyValuePair<string, string> email in emails)
        {
            if(!MvcUnitTst2.Tests.EmailService.SendEmail(email.Key,email.Value))
            {
                throw new Exception("Some message here");

            }
        }

    }



    private IEmailService EmailService { get; set; }
}


// the error is here:- send email is not implemented
    public class EmailService: IEmailService
    {
        public static bool SendEmail(string emailAddress,string message)
        {
            return false;
        }
    }

public interface IEmailService
{
    bool SendEmail(string emailAddress, string message);
}

}

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
Hunt
  • 1
  • 1

1 Answers1

2

You have your class implementing the IEmailService interface, but you have SendEmail marked as static.

Why doesn't c# allow static methods to implement an interface

Also, here you're trying to assign an instance to a class.

// error here:- class name is not valid at this point
MvcUnitTst2.Tests.EmailService = emailService;

Here's the fixed up code for these issues:

using System;
using System.Collections.Generic; // for the Dictionary
using NUnit.Framework;
using Moq;


namespace MvcUnitTst2.Tests
{
[TestFixture]
public class IntoToMoq
{
    [Test]
    public void NonMock()
    {

        var emailService = new EmailService();
        var emailer = new Emailer(emailService);
        emailer.SendBatchEmails();

    }
}

public class Emailer
{
    public Emailer(IEmailService emailService)
    {
        this.EmailService = emailService;
    }
    public void SendBatchEmails()
    {
        // build a list of emails
        // code below fails
        //Dictionary<string, string> emails = new Dictionary<string, string>
        //                                        {
        //                                            {"fred1@foo.com","Hello 1"},
        //                                            {"fred2@foo.com","Hello 2"},
        //                                            {"fred3@foo.com","Hello 3"},
        //                                            {"fred4@foo.com","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"fred1@foo.com","Hello 1"},
                                                    {"fred2@foo.com","Hello 2"},
                                                    {"fred3@foo.com","Hello 3"},
                                                    {"fred4@foo.com","Hello 4"}
                                                };                                       



        foreach (KeyValuePair<string, string> email in emails)
        {
            if(!this.EmailService.SendEmail(email.Key,email.Value))
            {
                throw new Exception("Some message here");

            }
        }

    }



    private IEmailService EmailService { get; set; }
}


    public class EmailService: IEmailService
    {
        public bool SendEmail(string emailAddress,string message)
        {
            return false;
        }
    }

public interface IEmailService
{
    bool SendEmail(string emailAddress, string message);
}
Community
  • 1
  • 1
Sam Trost
  • 2,173
  • 20
  • 26
  • Thanks Sam, That has help me a lot :) The intelisense in ReSharper put the MvcUnitTst2.Tests. in front of EmailService. So like a fool I took the offered code. That forced me to add the static... and from there on I was lost :( – Hunt Jan 21 '10 at 10:15