WCF呼び出しから返されたこの例外をどのようにテストできますか?
私はこのエラークラスを持っています。
[Serializable]
public class PermissionDenied_Error : Exception
{
public PermissionDenied_Error() : base("You are not approved.") { }
}
私のサービスでは、私はそれを投げています。
if (notApproved)
{
throw new FaultException(new PermissionDenied_Error()
, new FaultReason("Permissions Denied!"));
}
私のテストでは、私はそれを期待しています。
[Test]
[ExpectedException(typeof(FaultException))]
現在の結果は次のとおりです。
Expected: System.ServiceModel.FaultException`1[[PermissionDenied_Error
, Project.API, Version=1.0.4318.24332, Culture=neutral, PublicKeyToken=null]]
but was: System.ServiceModel.FaultException : Permissions Denied!
あなたの PermissionDenied_Error
はデータ契約でなければなりません。それは例外から派生すべきではありません。
また、 FaultContractAttribute
をオペレーションコントラクトに配置して、クライアントが例外を予期することがわかっている必要があります。
バラマスによって追加された
public interface IAccess
{
[OperationContract]
[FaultContract(typeof(PermissionDenied_Error))]
DtoResponse Access(DtoRequest request);
}
WCFでは単純にそのようには機能しません。
there are articles around explaining how to setup and use FaultExceptions
check this step by step here: Exception Handling in Windows Communication Framework and Best Practices
ExpectedException属性の使用は悪い習慣です。これを使用する代わりに:
[Test]
[ExpectedException(typeof(FaultException))]
試してみてください:
[Test]
public void Test1()
{
.....
try{
WfcServiceCall(...);
Assert.Fail("a FaultException was expected!");
}catch(FaultException){
Assert.Sucess();
}catch(Exception e){
Assert.Fail("Unexpected exception!")
}
}