2010-01-20 12 views
0

我試圖去抓住通過觀看這個演示 http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq 嘲諷和ReSharper的我敢肯定,我遵循正確的代碼,但我得到的錯誤。 你能給我一些指點,因爲我出錯了嗎?問題得到一個嘲諷的演示工作(NUnit的和ReSharper的)

錯誤:-class名字是不是在這一點上 錯誤有效: - 發送郵件未實現

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

}

回答

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> 
     //          { 
     //           {"[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); 
} 
+0

感謝薩姆, 這已幫助了我很多:) 在ReSharper的的智能感知把MvcUnitTst2.Tests。在EmailService之前。所以就像一個傻瓜,我提供了代碼。這迫使我添加靜態...從那裏我失去了:( – Hunt 2010-01-21 10:15:34