模拟演示工作时出现问题(Nunit 和 ReSharper)

发布于 2024-08-18 17:01:43 字数 3672 浏览 3 评论 0原文

我试图通过观看此演示来掌握模拟和 ReSharper http://www.bestechvideos。 com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq 我确信我正确地遵循了代码,但出现了错误。 您能否给我一些指示,告诉我我是否出错了?

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

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>
        //                                        {
        //                                            {"[email protected]","Hello 1"},
        //                                            {"[email protected]","Hello 2"},
        //                                            {"[email protected]","Hello 3"},
        //                                            {"[email protected]","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"[email protected]","Hello 1"},
                                                    {"[email protected]","Hello 2"},
                                                    {"[email protected]","Hello 3"},
                                                    {"[email protected]","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);
}

}

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>
        //                                        {
        //                                            {"[email protected]","Hello 1"},
        //                                            {"[email protected]","Hello 2"},
        //                                            {"[email protected]","Hello 3"},
        //                                            {"[email protected]","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"[email protected]","Hello 1"},
                                                    {"[email protected]","Hello 2"},
                                                    {"[email protected]","Hello 3"},
                                                    {"[email protected]","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);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-08-25 17:01:43

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

为什么 c# 不允许静态方法实现接口

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

// 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>
        //                                        {
        //                                            {"[email protected]","Hello 1"},
        //                                            {"[email protected]","Hello 2"},
        //                                            {"[email protected]","Hello 3"},
        //                                            {"[email protected]","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"[email protected]","Hello 1"},
                                                    {"[email protected]","Hello 2"},
                                                    {"[email protected]","Hello 3"},
                                                    {"[email protected]","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);
}

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>
        //                                        {
        //                                            {"[email protected]","Hello 1"},
        //                                            {"[email protected]","Hello 2"},
        //                                            {"[email protected]","Hello 3"},
        //                                            {"[email protected]","Hello 4"}
        //                                        };

        // use this instead
        var emails = new Dictionary<string, string>
                                                {
                                                    {"[email protected]","Hello 1"},
                                                    {"[email protected]","Hello 2"},
                                                    {"[email protected]","Hello 3"},
                                                    {"[email protected]","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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文