【问题标题】:Problems getting a mocking demo working (Nunit and ReSharper)使模拟演示工作的问题(Nunit 和 ReSharper)
【发布时间】:2010-01-20 16:01:46
【问题描述】:

我正在尝试通过观看此演示来掌握模拟和 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>
        //                                        {
        //                                            {"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);
}

}

【问题讨论】:

    标签: unit-testing nunit mocking


    【解决方案1】:

    您的类实现了 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);
    }
    

    【讨论】:

    • 谢谢 Sam,这对我有很大帮助 :) ReSharper 中的 intelisense 放入了 MvcUnitTst2.Tests。在电子邮件服务前面。所以我像个傻瓜一样接受了提供的代码。这迫使我添加静态...从那里我迷路了:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多