2016-09-19 18 views
0

VBでテストするコードは次のようになります。簡体字VBでVBでMOQからC#を設定する

Public Interface IFwCompressor 
    Function Calculate(ByVal condenserPower As Double, 
        ByVal evaporatingTemp As Double, 
        ByVal condensingTemp As Double, 
        ByRef rotationalSpeed As Double, 
        ByRef compressorPower As Double, 
        ByRef electricalPower As Double) As CalculationResult 

    Enum CalculationResult 
    ActivateNextCircuit = 3 
    Off = 2 
    Ok = 0 
    UnknownError = -1 
    MaxRps = -6 
    End Enum 
End Interface 

Public Class Compressor 
    Private ReadOnly _fwCompressor As IFwCompressor 

    Public Sub New(ByVal fwCompressor As IFwCompressor) 
    _fwCompressor = fwCompressor     
    End Sub 

    Public Function CalculateIntermittentResult(ByVal compressorInput As CompressorIntermittenInput) As StatusAndResult(Of CompressorStatus, CompressorResult) 

    Dim meanCompressorPower, meanRotationalSpeed, meanElectricalPower As Double 

    Dim result = _fwCompressor.CalculateIntermittentResult(_ 
     compressorInput.RotationalSpeed, 
     compressorInput.RunningTimeFraction, 
     compressorInput.CompressorPower, 
     meanRotationalSpeed, 
     meanCompressorPower, 
     meanElectricalPower) 

    Return New StatusAndResult(Of CompressorStatus, CompressorResult)(
     CompressorStatus.Ok, 
     New CompressorResult(CompressorRunMode.Intermittent, 
          meanRotationalSpeed, 
          meanCompressorPower, 
          meanElectricalPower)) 
End Function 

私がこのように書いたテストです。 C#とMOQフレームワークです。

double meanRotationalSpeed = 15; 
double meanCompressorPower = 1000; 
double meanElectricalPower = 500; 

fwCompressor.Setup(e => e.CalculateIntermittentResult(It.IsAny<double>(), 
                 It.IsAny<double>(), 
                 It.IsAny<double>(), 
                 ref meanRotationalSpeed, 
                 ref meanCompressorPower, 
                 ref meanElectricalPower)).Returns(MaxRps); 

私の問題は、メソッドが取得するときには、CalculateIntermittentResult内のパラメータmeanRotationalSpeedMeanCompressorPowerMeanElectricalPowerと結果の戻り0を呼び出すことですか?

ByRef MOQのパラメータは、C#からVBに可能ですか?

+0

どのバージョンのMoqを使用していますか? – Spock

+0

お使いのMoqのバージョンは? = 4.5.1 –

+0

以下の答えに感謝します。Moq> 4はref引数をサポートしています。 – Spock

答えて

0

このMOQの拡張子と

.OutCallback((double d1, 
        double d2, 
        double d3, 
        out double v1, 
        out double v2, 
        out double v3) => 
       { 
        v1 = 15; 
        v2 = 1000; 
        v3 = 500; 
       }).Returns(IFwCompressor.CalculationResult.MaxRps);.OutCallback((double d1, 
        double d2, 
        double d3, 
        out double v1, 
        out double v2, 
        out double v3) => 
       { 
        v1 = 15; 
        v2 = 1000; 
        v3 = 500; 
       }).Returns(IFwCompressor.CalculationResult.MaxRps); 



namespace MoqExtensions 
{ 
using Moq.Language; 
using Moq.Language.Flow; 
using System.Reflection; 

public static class MoqExtensions 
{ 
    public delegate void OutAction<TOut>(out TOut outVal); 
    public delegate void OutAction<in T1, TOut>(T1 arg1, out TOut outVal); 
    public delegate void OutAction<in T1, TOut1, TOut2>(T1 arg1, out TOut1 outVal1, out TOut2 outVal2); 
    public delegate void OutAction<in T1, in T2, in T3, TOut1, TOut2, TOut3>(T1 arg1, T2 arg2, T3 agr3, out TOut1 outVal1, out TOut2 outVal2, out TOut3 outVal3); 

    public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, TOut>(this ICallback<TMock, TReturn> mock, OutAction<TOut> action) 
     where TMock : class 
    { 
     return OutCallbackInternal(mock, action); 
    } 

    public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, T1, T2, T3, TOut1, TOut2, TOut3>(this ICallback<TMock, TReturn> mock, OutAction<T1, T2, T3, TOut1, TOut2, TOut3> action) 
    where TMock : class 
    { 
     return OutCallbackInternal(mock, action); 
    } 

    public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, T1, TOut1, TOut2>(this ICallback<TMock, TReturn> mock, OutAction<T1, TOut1, TOut2> action) 
     where TMock : class 
    { 
     return OutCallbackInternal(mock, action); 
    } 

    public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, T1, TOut>(this ICallback<TMock, TReturn> mock, OutAction<T1, TOut> action) 
     where TMock : class 
    { 
     return OutCallbackInternal(mock, action); 
    } 

    private static IReturnsThrows<TMock, TReturn> OutCallbackInternal<TMock, TReturn>(ICallback<TMock, TReturn> mock, object action) 
     where TMock : class 
    { 
     mock.GetType() 
      .Assembly.GetType("Moq.MethodCall") 
      .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, 
       new[] { action }); 
     return mock as IReturnsThrows<TMock, TReturn>; 
    } 
} 
にコードを変更することも参照4 https://github.com/moq/moq4

"アウト/ refの引数のための直感的な支援" 部品番号の新機能を参照してください。

}

0

とにかく質問に答えるべきだと思います。最新バージョンのMoq(バージョン4)を使用している場合は、ref引数をサポートしています。ドキュメンテーションを見ると、引数が引数としてSystem Under Tests内で呼び出されたときに渡すのと同じインスタンスである限り、ref引数の設定がサポートされます。

https://github.com/Moq/moq4/wiki/Quickstart

// ref arguments 
    var instance = new Bar(); 
    // Only matches if the ref argument to the invocation is the same instance 
    mock.Setup(foo => foo.Submit(ref instance)).Returns(true); 

あなたは二重のデータ型を使用している場合、それはあなたがテスト中のシステムで使用するのと同じ値である必要があります。

//method under test 
    public int CallRef() 
    { 
     double d1 = 10; 
     var r1 = _foo.DoRef(ref d1); 

     return r1; 
    } 

    //test method 
    [TestMethod] 
    public void TestMethod1() 
    { 
     double dt = 10; 
     var fooStub = new Mock<IFoo>(); 
     fooStub.Setup(x => x.DoRef(ref dt)).Returns(7); 

     var s = new S(fooStub.Object); 

     var r1 = s.CallRef(); 
    } 

dtが10である限り、REFの設定が機能するはずです。

Moq 3を使用している場合、refパラメータはサポートされていないと思います。

Assigning out/ref parameters in Moq

関連する問題