【解決方法】Moqコンストラクターの質問


クラスがあります。

C#
public class VoiceElementsLine : ILine
   {
       public Guid LineId { get; set; }
       public TelephonyServer TelephonyServer { get; set; }
       public virtual ChannelResource ChannelResource { get; set; }
       public LanguageData LangInfo { get; set; }

       public string InboundDnis
       {
           get { return _inboundDnis; }
       }

       private readonly Logger _appLog = GetCurrentLogger();
       private SipChannel _incomingSipChannel;
       public VoiceResource IncomingVoiceResource { get; private set; }
       private ServerInfo _serverInfo;
       private string _inboundDnis, _inboundAni, _inboundIp;
       private DateTime _callDate;

       public void RunCallScript()
       {
           SetupLine();
           AnswerSipChannel();
           var callFlowApp = GetCallFlowApplication();
           callFlowApp.HandleCall();
           Hangup();
       }

       private void AnswerSipChannel()
       {
           _incomingSipChannel.Answer();
       }

       public ICallFlowApplication GetCallFlowApplication()
       {
           var inboundValues = _inboundDnis.Split('_');
           var appId = byte.Parse(inboundValues[0]);

           var appFactory = GetApplicationFactoryInstance();
           var callFlowApp = appFactory.GetApplication(appId);
           return callFlowApp;
       }

       public IApplicationFactory GetApplicationFactoryInstance()
       {
           return VoiceElementsCallFlowService.Container.GetInstance<IApplicationFactory>();
       }

       public void PlayPromptTextToSpeech(string promptText)
       {
           IncomingVoiceResource.PlayTTS(promptText);
       }

       public void PlayPromptFile(string promptFile)
       {
           IncomingVoiceResource.Play(promptFile);
       }

       public string GetUserInput(int maxDigits)
       {
           IncomingVoiceResource.MaximumDigits = maxDigits;
           return IncomingVoiceResource.DigitBuffer;
       }

       private void in_ChannelResource_Disconnected(object sender, DisconnectedEventArgs e)
       {
           _appLog.Info("Inbound Disconnect Event");
       }

       public void SetupLine()
       {
           try
           {
               _incomingSipChannel = ChannelResource as SipChannel;
               _incomingSipChannel.Disconnected += in_ChannelResource_Disconnected;
               _serverInfo = TelephonyServer.GetServerInfo();
               IncomingVoiceResource = _incomingSipChannel.VoiceResource;
               IncomingVoiceResource.Codec = Codec.PCM_8Khz_8Bit;

               _inboundDnis = ChannelResource.Dnis;
               // Ex: if dialstring is facilityidInmatePIN@10.50.96.222
               // then InboundDnis = languagefacilityIdInmatePIN; 1_1_163_1234@10.50.96.222
               _inboundAni = ChannelResource.Ani;
               _inboundIp = _incomingSipChannel.RemoteCallControlAddress;
               _callDate = DateTime.Now;

               LogSetupLineInformation();
           }
           catch (Exception ee)
           {
               _appLog.Error(ee);
           }
       }

対応するインターフェース ILine

C#
public interface ILine
   {
       Guid LineId { get; set; }

       void RunCallScript();
       void PlayPromptTextToSpeech(string promptText);
       void PlayPromptFile(string promptFile);
       string GetUserInput(int maxDigits);
       void Hangup();
   }

私の単体テストでは、まずメソッドを確認したい SetupLine 実行されます。 だから私はMoqを使います。

C#
[Fact]
        public void SetupLine_Should_Properly_Setup_Codec()
        {
            // ARRANGE
            Mock<VoiceElementsLine> voiceElementsLineMock = new Mock<VoiceElementsLine>()
            { CallBase = true};
            Mock<TelephonyServer> telephonyServerMock = new Mock<TelephonyServer>();
            Mock<ChannelResource> channelResourceMock = new Mock<ChannelResource>(telephonyServerMock) ;
            voiceElementsLineMock.SetupGet(x => x.ChannelResource).Returns(channelResourceMock.Object);
            var voiceElementsLine = voiceElementsLineMock.Object;

            // ACT
            voiceElementsLine.SetupLine();

            // ASSERT
            Assert.True(voiceElementsLine.IncomingVoiceResource.Codec== Codec.PCM_8Khz_8Bit);
        }

注意してください ChannelResource コンストラクターがあります TelephonyServer.
ただし、エラーが発生しました

C#
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code


Thanks for help with the code and hints.

解決策 1

あなたのクラスだと思います

C#
ChannelResource

デフォルトのコンストラクターまたは空を持たないでください。

C#
public void ChannelResource(){}

解決策 2

インターフェイスのすべてのメソッドを実装する必要があります。 public void Hangup(){} はまだ実装されていません

解決策 3

テストでは、具体的なクラス「VoiceElementsLine」をモックしています。 具体的なクラスではなく、インターフェースをモックする必要があります(ただし、具体的なクラスにパブリックのデフォルトコンストラクターがある場合は、Moq で行うことができます)。

したがって、テストでは次を使用する必要があります。

C#
Mock<ILine> voiceElementsLineMock = new Mock<ILine>() { CallBase = true};

コメント

タイトルとURLをコピーしました