2011-05-16 2 views
7

는이 같은 클래스가 :Moles를 생성자에 사용하는 방법은 무엇입니까?

public class Product : IProduct 
{ 
    static private string _defaultName = "default"; 
    private string _name; 
    private float _price; 
    /// Constructor 
    public Product() 
    { 
     _price = 10.0F; 
    } 
    public void ModifyPrice(float modifier) 
    { 
     _price = _price * modifier; 
    } 

내가 특정 값에 대해 아무것도 할 ModifyPrice 원하는, 그러나 나는 또한이 같은 것을 시도 10으로 가격을 설정 생성자를 호출 할 :

var fake = new SProduct() { CallBase = true }; 
var mole = new MProduct(fake) 
    { 
     ModifyPriceSingle = (actual) => 
     { 
      if (actual != 20.0f) 
      { 
       MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual)); 
      } 
     } 
    }; 
MProduct.Constructor = (@this) => (@this) = fake; 

그러나 경우에도 가짜

이 좋은 생성자 잘 초기화, 나는 @this에 할당 할 수 없습니다. 나는 또한 같은 것을 시도한다

MProduct.Constructor = (@this) => { var mole = new MProduct(@this)... }; 

그러나 이번에는 내 생성자를 호출 할 수 없다. 어떻게해야합니까?

답변

5

생성자를 조롱 할 필요가 없으며 Product 클래스의 매개 변수가없는 생성자가 이미 원하는 것을 수행합니다.

일부 디버깅 출력을 Product에 추가하십시오.

public class Product 
{ 
    private float _price; 
    public Product() 
    { 
     _price = 10.0F; 
     Debug.WriteLine("Initializing price: {0}", _price); 
    } 
    public void ModifyPrice(float modifier) 
    { 
     _price = _price*modifier; 
     Debug.WriteLine("New price: {0}", _price); 
    } 
} 

모의 만 ModifyPrice 방법.

var fake = new SProduct { CallBase = true }; 

는 인스턴스를 생성, 그런데

 
    Initializing price: 10 
    Skipped setting price. 
    New price: 210 

, 당신은 여기에 그루터기를 사용할 필요가 없습니다 :

[TestMethod] 
[HostType("Moles")] 
public void Test1() 
{ 
    // Call a constructor that sets the price to 10. 
    var fake = new SProduct { CallBase = true }; 
    var mole = new MProduct(fake) 
    { 
     ModifyPriceSingle = actual => 
     { 
      if (actual != 20.0f) 
      { 
       MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual)); 
      } 
      else 
      { 
       Debug.WriteLine("Skipped setting price."); 
      } 
     } 
    }; 
    fake.ModifyPrice(20f); 
    fake.ModifyPrice(21f); 
} 

모든 것을 확인하는 디버그 출력이 예상대로 작동보기 Product이면 충분합니다.

var fake = new Product(); 

업데이트 : 하나의 방법 비웃음

MProduct.Behavior = MoleBehaviors.Fallthrough; 
MProduct.AllInstances.ModifyPriceSingle = (p, actual) => 
{ 
    if (actual != 20.0f) 
    { 
     MolesContext.ExecuteWithoutMoles(() => p.ModifyPrice(actual)); 
    } 
    else 
    { 
     Debug.WriteLine("Skipped setting price."); 
    } 
}; 

// Call the constructor that sets the price to 10. 
Product p1 = new Product(); 
// Skip setting the price. 
p1.ModifyPrice(20f); 
// Set the price. 
p1.ModifyPrice(21f); 
+0

흠처럼 AllInstances 클래스를 달성 할 수있다, 당신은 내 질문을 이해하지 못했다. 귀하의 모범은 좋은 지적입니다. 그러나, 나는 ModifyPrice를 제외하고 원래의 메소드를 호출하는 Product의 mock이 필요하다. 그래서 CallBase = true (스텁의 경우) 또는 InstanceBehavior = MoleBehaviors.FallThrough (첩자의 경우)가 필요한 이유입니다. 게다가, 이것이 가장 중요한 요점입니다. 나는 미래의 모든 제품을 내 모의로 늪에 빠뜨리고 싶습니다! 그래서 생성자를 조롱해야합니다. – Jeco

+1

@Jeco 다시 당신을 오해했을 수도 있지만, AllInstances 클래스로 원하는 것을 얻을 수있는 것처럼 보입니다. – Gebb

+0

여전히 좋은 답변입니다. 불완전한 예를 들어 사과 드리며 항상 다른 점을 염두에두고 있습니다. 몇 가지 조롱 프레임 워크를 두더지와 비교하면서 모든 가능성을 연구하려고했습니다. Moles를 사용하여 객체의 모든 미래 인스턴스를 늪에 빠뜨리는 것은 좋은 생각입니다 (모든 메소드 호출을 검사하려고 시도했으며 'NonInmplementedException'과 비교하여 'AllInstances'로 많이 표현했습니다). 나는 생성자를 필사적으로 사용하여 '@ this = aMole'을하려고 노력했다. 그러나 Moles 매뉴얼에서 말했듯이, 그것은 고립 프레임 워크이며 모의 틀이 아니다. 답변 주셔서 감사합니다 – Jeco

0
MProduct.Behavior = MoleBehaviors.Fallthrough; 
+0

매우 간결한 답변 ... –

관련 문제