让模拟演示工作的问题(Nunit和ReSharper)

时间:2010-01-20 16:01:46

标签: unit-testing nunit mocking

我正试图通过观看此演示来掌握模拟和ReSharper http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq 我确定我正确地遵循了代码但是我遇到了错误。 你能给我一些关于我出错的指示吗?

错误:-class name此时无效 错误: - 未实施电子邮件

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);
}

}

1 个答案:

答案 0 :(得分:2)

您的类实现了IEmailService接口,但您将SendEmail标记为静态。

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

此外,您正在尝试将一个实例分配给一个类。

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

以下是针对这些问题的修正代码:

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);
}