在F#Foq中模拟非标准事件 [英] Mocking Non-Standard Events in F# Foq

查看:53
本文介绍了在F#Foq中模拟非标准事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是F#的新手,我正在通过一些练习来学习该语言.我目前正在尝试的是为自定义Castle.Windsor设施编写单元测试,而我正在尝试模拟内核以引发"ComponentRegistered"事件.

I'm new to F#, and I'm putting myself through some exercises to learn the language. The one I'm currently trying to do is to write a unit test for a custom Castle.Windsor Facility, and I am trying to Mock the Kernel to raise a "ComponentRegistered" event.

我正在使用的工具是FsUnit/xUnit/Foq.

The tools I'm using are FsUnit/xUnit/Foq.

我的代码:

let event = Event<_,_>()

let kernel = Mock<IKernel>()
    .SetupEvent(fun k -> <@ k.ComponentRegistered @>)
    .Publishes(event.Publish)
    .Create()

错误消息:

错误4事件"ComponentRegistered"具有非标准类型.如果此事件是使用另一种CLI语言声明的,您可能需要访问使用显式的add_ComponentRegistered和事件的remove_ComponentRegistered方法.如果这个事件是在F#中声明,使事件的类型成为以下两者的实例化'IDelegateEvent< >'或'IEvent< ,_>'.C:\ Workbench \ EvilDev \ evildev.commons \ Tests \ EvilDev.Commons.Windsor.Tests \ Auto解析程序工具规范.fs35 53 EvilDev.Commons.Windsor.Tests

Error 4 The event 'ComponentRegistered' has a non-standard type. If this event is declared in another CLI language, you may need to access this event using the explicit add_ComponentRegistered and remove_ComponentRegistered methods for the event. If this event is declared in F#, make the type of the event an instantiation of either 'IDelegateEvent<>' or 'IEvent<,_>'. C:\Workbench\EvilDev\evildev.commons\Tests\EvilDev.Commons.Windsor.Tests\Auto Resolver Facility Specification.fs 35 53 EvilDev.Commons.Windsor.Tests

如何从F#中模拟/触发此类事件?

How do I mock/trigger this sort of event from F#?

推荐答案

可以使用

可以使用我已经扩展了Foq API,以允许按名称指定事件,因此您可以指定事件名称(无类型)以解决非标准事件类型编译器错误.

I have extended the Foq API to allow events to be specified by name, so that you can specify the event name (untyped) to workaround the non-standard event type compiler error.

已提交用于添加新SetupEventByName方法的更改集.如果您现在需要此功能,则可以从源代码构建,也可以简单地添加最新的 Foq.fs 转到您的项目.该功能将在Foq 1.5中提供,我将在第二天左右尝试使用该功能.

The change set to add the new SetupEventByName method has been committed. If you need this right now you can either build from source or simply add the latest Foq.fs to your project. This feature will be in Foq 1.5 which I will try and push in the next day or so.

这篇关于在F#Foq中模拟非标准事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆