Unit testing 使模拟演示正常工作时出现问题(Nunit和ReSharper)

Unit testing 使模拟演示正常工作时出现问题(Nunit和ReSharper),unit-testing,nunit,mocking,Unit Testing,Nunit,Mocking,我试图通过观看这个演示来掌握mocking和ReSharper 我确信我正确地遵循了代码,但是我遇到了错误。 你能告诉我我是不是出了问题吗 错误:-类名此时无效 错误:-未实现发送电子邮件 using System; using System.Collections.Generic; // for the Dictionary using NUnit.Framework; using Moq; namespace MvcUnitTst2.Tests { [TestFixture] publi

我试图通过观看这个演示来掌握mocking和ReSharper 我确信我正确地遵循了代码,但是我遇到了错误。 你能告诉我我是不是出了问题吗

错误:-类名此时无效 错误:-未实现发送电子邮件

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);
}
使用系统;
使用System.Collections.Generic;//查字典
使用NUnit.Framework;
使用最小起订量;
命名空间MvcUnitTst2.Tests
{
[测试夹具]
公共课
{
[测试]
公开无效非股票()
{
var emailService=new emailService();
var emailer=新的emailer(emailService);
emailer.SendBatchEmails();
}
}
公共类电子邮件发送程序
{
公共电子邮件发送程序(IEmailService电子邮件服务)
{
//此处错误:-类名此时无效
MvcUnitTst2.Tests.EmailService=EmailService;
}
公共无效SendBatchEmails()
{
//建立一个电子邮件列表
//下面的代码失败
//字典电子邮件=新字典
//                                        {
//                                            {"fred1@foo.com“,“你好1”},
//                                            {"fred2@foo.com“,“你好2”},
//                                            {"fred3@foo.com“,“你好3”},
//                                            {"fred4@foo.com“,“你好4”}
//                                        };
//用这个代替
var=新字典
{
{"fred1@foo.com“,“你好1”},
{"fred2@foo.com“,“你好2”},
{"fred3@foo.com“,“你好3”},
{"fred4@foo.com“,“你好4”}
};                                       
foreach(电子邮件中的KeyValuePair电子邮件)
{
if(!MvcUnitTst2.Tests.EmailService.SendEmail(email.Key,email.Value))
{
抛出新异常(“此处有消息”);
}
}
}
专用IEmailService电子邮件服务{get;set;}
}
//错误在此处:-未执行发送电子邮件
公共类电子邮件服务:IEmailService
{
公共静态bool sendmail(字符串emailAddress,字符串消息)
{
返回false;
}
}
公共接口IEmailService
{
bool sendmail(字符串emailAddress,字符串message);
}

}您的类实现了IEmailService接口,但您的sendmail标记为static

另外,这里您尝试将一个实例分配给一个类

// 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);
}
使用系统;
使用System.Collections.Generic;//查字典
使用NUnit.Framework;
使用最小起订量;
命名空间MvcUnitTst2.Tests
{
[测试夹具]
公共课
{
[测试]
公开无效非股票()
{
var emailService=new emailService();
var emailer=新的emailer(emailService);
emailer.SendBatchEmails();
}
}
公共类电子邮件发送程序
{
公共电子邮件发送程序(IEmailService电子邮件服务)
{
this.EmailService=EmailService;
}
公共无效SendBatchEmails()
{
//建立一个电子邮件列表
//下面的代码失败
//字典电子邮件=新字典
//                                        {
//                                            {"fred1@foo.com“,“你好1”},
//                                            {"fred2@foo.com“,“你好2”},
//                                            {"fred3@foo.com“,“你好3”},
//                                            {"fred4@foo.com“,“你好4”}
//                                        };
//用这个代替
var=新字典
{
{"fred1@foo.com“,“你好1”},
{"fred2@foo.com“,“你好2”},
{"fred3@foo.com“,“你好3”},
{"fred4@foo.com“,“你好4”}
};                                       
foreach(电子邮件中的KeyValuePair电子邮件)
{
if(!this.EmailService.sendmail(email.Key,email.Value))
{
抛出新异常(“此处有消息”);
}
}
}
专用IEmailService电子邮件服务{get;set;}
}
公共类电子邮件服务:IEmailService
{
public bool sendmail(字符串emailAddress,字符串message)
{
返回false;
}
}
公共接口IEmailService
{
bool sendmail(字符串emailAddress,字符串message);
}

谢谢Sam,这对我帮助很大:)ReSharper中的Intellisense将MvcUnitTst2.Tests.放在EmailService前面。所以像个傻瓜一样,我接受了提供的代码。这迫使我添加了静态…从那以后,我迷失了方向:(