2012-06-21 55 views
3

我已經創建了一個Person類此C#擴展方法:C# - 如何測試擴展方法?

public static class PersonExtensions { 
    public static void Rename(this Person person, String newName) { 
     person.Name = newName; 
    } 
} 

我將如何單元測試這種方法?我試過了,但PersonAccessor對象不提供Rename方法。

錯誤是「私人訪問的重命名未找到」

當我嘗試PersonExtensions_Accessor.Rename(somePerson,newName)將,它說:「有一些無效參數」

+1

擴展方法是否存儲在您未引用的名稱空間中? –

+1

「PersonAccessor」類是否位於不同的名稱空間中?如果是這樣,您需要使用包含「PersonExtensions」的名稱空間 – ean5533

+1

與任何擴展方法一樣,您可以像訪問任何靜態方法一樣訪問該方法,因爲這就是它的真正原因。你應該可以做一些像'PersonExtensions.Rename(myPersonInstance,myNewName)'。 –

回答

1

這裏是我的生產代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ClassLibrary1 
{ 
    public class Person 
    { 
     public string Name { get; set; } 
    } 

    public static class PersonExtensions 
    { 
     public static void Rename(this Person person, String newName) 
     { 
     person.Name = newName; 
     } 
    } 
} 

這裏生成的測試的編輯版本:

using ClassLibrary1; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System; 

namespace TestProject1 
{ 


    /// <summary> 
    ///This is a test class for PersonExtensionsTest and is intended 
    ///to contain all PersonExtensionsTest Unit Tests 
    ///</summary> 
    [TestClass()] 
    public class PersonExtensionsTest 
    { 


     private TestContext testContextInstance; 

     /// <summary> 
     ///Gets or sets the test context which provides 
     ///information about and functionality for the current test run. 
     ///</summary> 
     public TestContext TestContext 
     { 
     get 
     { 
      return testContextInstance; 
     } 
     set 
     { 
      testContextInstance = value; 
     } 
     } 

     #region Additional test attributes 
    // 
    //You can use the following additional attributes as you write your tests: 
    // 
    //Use ClassInitialize to run code before running the first test in the class 
    //[ClassInitialize()] 
    //public static void MyClassInitialize(TestContext testContext) 
    //{ 
    //} 
    // 
    //Use ClassCleanup to run code after all tests in a class have run 
    //[ClassCleanup()] 
    //public static void MyClassCleanup() 
    //{ 
    //} 
    // 
    //Use TestInitialize to run code before running each test 
    //[TestInitialize()] 
    //public void MyTestInitialize() 
    //{ 
    //} 
    // 
    //Use TestCleanup to run code after each test has run 
    //[TestCleanup()] 
    //public void MyTestCleanup() 
    //{ 
    //} 
    // 
    #endregion 


     /// <summary> 
     ///A test for Rename 
     ///</summary> 
     [TestMethod()] 
     public void RenameTest() 
     { 
     Person person = new Person(); // TODO: Initialize to an appropriate value 
     string newName = string.Empty; // TODO: Initialize to an appropriate value 
     PersonExtensions.Rename(person, newName); // this could also be written as person.Rename(newName); 
     Assert.AreEqual(person.Name, string.Empty); 
     } 
    } 
} 

測試通過。

+0

我無法創建一個'Person',只有一個'PersonAccessor'。同樣,我不能直接訪問'PersonExtensions',而必須使用'PersonExtensions_Accessor'。 – ryyst

+0

@ryyst我已經使用了Visual Studio 2010的生成測試功能,並且它生成了一個引發null-ref的測試。我編輯了我的答案,包括通過考試。 – GregC

+0

@ryyst代碼中Person和PersonAccessor的聲明有什麼區別?什麼是命名空間,什麼是訪問說明符? (嘗試使用反射器或對象瀏覽器) – GregC

8

擴展方法只是用於引用靜態方法的不同方法的語法糖。請在您的單元測試中撥打PersonExtensions.Rename(...)

+0

該方法擴展了Person,而不是PersonExtensions。 – ean5533

+1

如果您不使用正在擴展的類,它將無法創建擴展方法。如果它叫'新人(...),測試會更準確。重命名(newName)' – ean5533

+0

它說:「沒有找到重命名的私人訪問器」。那是什麼意思? 當我嘗試'PersonExtensions_Accessor.Rename(somePerson,newName)'時,它說「有一些無效的參數」。 – ryyst

3

我認爲一個好的方法可以直接測試Person實例中的擴展方法。

考慮到你制定的方法,樣本代碼將是這樣的:

using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using PersonExtension; // Don't forget about reference it 

namespace UnitTest { 
    [TestClass] 
    public class UnitTest { 
     Person person; 
     [TestInitialize] 
     public void Init() { 
      person = new Person("Person name"); 
     } 

     [TestMethod] 
     public void TestRename() { 
      Assert.AreEqual("Person name", person.Name); 
      person.Rename("New name"); 
      Assert.AreEqual("New name", person.Name); 
     } 
    } 
} 

記住同時引用PersonPersonExtension類和有才能在實用類的正確隱藏的水平可以訪問其方法