여기 내 문제가 있습니다 :이 경우 모의 작업을 사용하는 방법은 무엇입니까?
나는 단위 테스트를 작성해야하는 n 계층 응용 프로그램이 있습니다. 단위 테스트는 비즈니스 계층에 대한 테스트입니다.
이 테스트에는 Insert()
이라는 메서드가 있으며이 메서드는 상속의 두 보호 된 메서드를 사용하고 데이터 액세스 계층의 메서드를 직접 호출합니다.
그래서 DAL 용 mock 개체를 만들었습니다. 그러나 여기에 요점이있다, (편집 :)에서 메서드를 상속에서 보호, 그것은 DAL에서 다른 개체를 사용합니다! 이걸 조롱하는 것은 불가능한 것 같습니다!
다음public int Insert(MYOBJECT aMyObject)
{
//first inherited method use the FIRSTDALOBJECT so the mock object --> No problem
aMyObject.SomeField= FirstInherited();
//Second inherited method (see after) --> my problem
aMyObject.SomeOtherField = SecondInherited();
// Direct access to DALMethod, use FIRSTDALOBJECT so the mock -->No Problem
return this.FIRSTDALOBJECT.Insert(aMyObject);
}
가 SecondInherited 방법입니다 :
protected string SecondInherited()
{
// Here is my problem, the mock here seems not be possible for seconddalobject
return (new SECONDDALOBJECT Sdo().Stuff());
}
그리고 여기에 단위 시험 방법 코드 :
[TestMethod()]
public void InsertTest()
{
BLLCLASS_Accessor target = new BLLCLASS_Accessor();
MYOBJECT aMyObject = new MYOBJECT { SomeField = null, SomeOtherField = 1 };
int expected = 1;
int actual;
//mock
var Mock = new Mock<DAL.INTERFACES.IFIRSTDALOBJECT>();
//Rec for calls
List<SOMECLASS> retour = new List<SOMECLASS>();
retour.Add(new SOMECLASS());
//Here is the second call (last from method to test)
Mock
.Setup(p => p.Insert(aMyObject))
.Returns(1);
// Here is the first call (from the FirstInherited())
Mock
.Setup(p => p.GetLast())
.Returns(50);
// Replace the real by the mock
target.demande_provider = Mock.Object;
actual = target.Insert(aMyObject);
Assert.AreEqual(/*Some assertion stuff*/);
}
감사합니다 여기
테스트 코드에 대한 방법입니다 모든 질문을 읽기 :-) 그것이 충분히 명확한 희망.
그것은 실수로 죄송합니다. – bAN
+1 : 빠른 결과를위한 좋은 해결책 –
@bAN, 해명 덕분에 해결책이 훨씬 쉽습니다. –