2012-04-03 13 views
7

Sto usando il framework Moq per il test delle unità e vorrei poter passare in Action per registrare i metodi void.Come posso creare un'azione <T> in F #?

let log = new Mock<ILog>() 
let quot = <@ fun (mock:ILog) -> mock.Info(It.IsAny<string>) @> 
let expr = (quot.ToLinqExpression() :?> Expression<Action<ILog>>) 
log.Verify(expr) 

Questo codice non riesce con il seguente errore:

System.InvalidCastException : Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.Expression 1[System.Action 1[log4net.ILog]]'.

posso stampare il tipo utilizzando

printfn "%s" (quot.Type.ToString()) 

che emette

Microsoft.FSharp.Core.FSharpFunc`2[log4net.ILog,Microsoft.FSharp.Core.Unit]

Così, come posso creare un'azione?

+1

Moq in F # -> probabilmente una cattiva idea . –

+1

Sì, in F #, noi Foq invece - vedi http://trelford.com/blog/post/Foq.aspx e http://trelford.com/blog/post/FoqItEasy.aspx (transizione molto facile da Moq) –

risposta

5

Prova:

let quot = <@ new Action<_>(fun (mock:ILog) -> mock.Info(It.IsAny<string>)) @> 
+0

Questo ha creato l'azione correttamente, ma ancora non ha funzionato per Moq. – YonahW

12

espressioni LINQ sono pienamente supportati in F # 3, così ora si può passare un azione per Moq come lambda expression:

let mock = Mock<ILog>() 
mock.Verify(fun (log:ILog) -> log.Info(It.IsAny<string>()))