VSTO单元测试 [英] VSTO Unit Testing

查看:78
本文介绍了VSTO单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题


我再次发布此问题(第3次)因为我的no

垃圾邮件别名的一些问题。

这是一个问题:

我无法为VSTO(2005)项目运行单元测试。我在同一个解决方案中有一个

Excel项目和一个单元测试项目。我还没有找到

初始化runtimecallback的方法,这是由visual studio生成的代码所需的
(见下面的声明)。我对单元测试进行了大量的接力,并且需要这个工作才能获得
。我希望我在这里遗漏了一些明显的东西。

请帮助。


''视觉工作室生成的代码

Dim RuntimeCallback As

Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProvider = Nothing''TODO:初始化为适当的值


Question

I am posting this question again (3rd time) because some issues with my no
spam alias.
Here it is the question:
I have not been able to run unit tests for a VSTO (2005) project. I have an
Excel project and a unit test project in the same solution. I have not found
the way to initialize the runtimecallback as required by the code generated
by visual studio (see statement below). I relay heavily on unit testing and
having this working is a must. I hope I am missing something obvious here.
Please help.

''code generated by visual studio
Dim RuntimeCallback As
Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProvider = Nothing ''TODO: Initialize to an appropriate value


推荐答案

在单元测试项目中,我们可以简单地创建一个类来补充

接口''IRuntimeServiceProvider'',并从这个

班级。例如:

为Test项目添加一个类:

Public Class Class1

Implements

Microsoft.VisualStudio .Tools.Applications.Runtime。 IRuntimeServiceProvider

公共函数GetService(ByVal serviceType As System.Type)作为对象

实现

Microsoft.VisualStudio.Tools.Applications.Runtime。 IRuntimeServiceProvider.Ge

t服务

不返回

结束功能


结束班级


并更改以下行:


Dim RuntimeCallback As

Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProv

ider = Nothing''TODO:初始化为适当的值

to:


Dim RuntimeCallback As IRuntimeServiceProvider = New Class1( )


如果还有其他问题,请随时告诉我们


卢克

In a Unit Test project, we can simple create a class inplementing the
interface ''IRuntimeServiceProvider'', and Initialize a object from this
class here. For example:
add a class to the Test project:
Public Class Class1
Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider
Public Function GetService(ByVal serviceType As System.Type) As Object
Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider.Ge
tService
Return Nothing
End Function

End Class

and change following line:

Dim RuntimeCallback As
Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProv
ider = Nothing ''TODO: Initialize to an appropriate value
to:

Dim RuntimeCallback As IRuntimeServiceProvider = New Class1()

If there is any further questions, please feel free to let us know

Luke





" Luke Zhang [MSFT]"写道:


"Luke Zhang [MSFT]" wrote:
在单元测试项目中,我们可以简单地创建一个类来补充
接口''IRuntimeServiceProvider'',并从这个类中初始化一个对象这里。例如:
为Test项目添加一个类:

公共类Class1
实现
Microsoft.VisualStudio.Tools.Applications.Runtime。 IRuntimeServiceProvider

公共函数GetService(ByVal serviceType As System.Type)作为对象
实现
Microsoft.VisualStudio.Tools.Applications.Runtime。 IRuntimeServiceProvider.Ge
tService
什么都不返回
结束功能

结束课程

并改变以下行:
Dim RuntimeCallback As
Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProv
ider = Nothing''TODO:初始化为适当的值

to:

Dim RuntimeCallback为IRuntimeServiceProvider = New Class1()

如果还有其他问题,请随时告诉我们

Luke
In a Unit Test project, we can simple create a class inplementing the
interface ''IRuntimeServiceProvider'', and Initialize a object from this
class here. For example:
add a class to the Test project:
Public Class Class1
Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider
Public Function GetService(ByVal serviceType As System.Type) As Object
Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider.Ge
tService
Return Nothing
End Function

End Class

and change following line:

Dim RuntimeCallback As
Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProv
ider = Nothing ''TODO: Initialize to an appropriate value
to:

Dim RuntimeCallback As IRuntimeServiceProvider = New Class1()

If there is any further questions, please feel free to let us know

Luke




卢克:我还有问题。我无法访问诸如

工作表集合之类的对象。例如:

extendedThisWorkbook.Worksheets.count失败,因为工作表集合是

null。我试过在ThisWorkbook对象中首先运行ThisWorkbook_startup

(扩展excel)并且有和没有excel运行


这是我的代码。请参阅说明正在发生的事情的说明。

请帮忙。


公共类Class1:Implements

Microsoft.VisualStudio.Tools .Applications.Runtime。 IRuntimeServiceProvider

公共函数GetService(ByVal serviceType As System.Type)As

Object Implements

Microsoft.VisualStudio.Tools.Applications.Runtime。 IRuntimeServiceProvider.GetService

不返回

结束功能


结束班级

< TestMethod() > _

Public Sub FirstTest()

''这是你电子邮件中的建议

Dim RuntimeCallback As

Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProvider = New Class1''TODO:初始化为适当的值


''这是由visual studio编写的。我将

变量的名称从target更改为extendedThisWorkbook。我更容易理解。

Dim extendedThisWorkbook As Global.ExcelTestWorkbook.ThisWorkbook =

新的Global.ExcelTestWorkbook.ThisWorkbook(RuntimeCallb ack)


''我的测试从这里开始


''在ExcelTestWorkbook项目的公共模块中运行方法

Global.ExcelTestWorkbook.Main()


''TestThisFunction是

ExcelTestWorkbook项目中公共模块中的一种方法

''如果Global,它将返回true .ExcelTestWorkbook.Main(之前的

语句)工作正常。

Dim actual As Boolean =

Global.ExcelTestWorkbook.Perro.TestThisFunction


''这不会失败,它工作正常。这意味着我对ExcelTestWorkbook项目中的公共模块有一个

引用。

Assert.IsTrue(实际)


''这不会失败,它工作正常。这意味着我在

ExcelTestWorkbook项目中对Thisworkbook(扩展工作簿)对象有一个

引用。

Assert.IsTrue(extendedThisWorkbook.IAmAlive)


''这里是我遇到问题的地方,任何下面的调用都会抛出

异常

''系统。 NullReferenceException异常。 extendedThisWorkbook为null。

''extendedThisWorkbook.InnerObject为null,同样适用于

extendedThisWorkbook.Worksheets和similars

Assert.AreEqual(5,extendedThisWorkbook .Worksheets.Count)

End Sub



Luke: I still have the problem. I can not access objects such as the
worksheet collection. For instance:
extendedThisWorkbook.Worksheets.count fails because worksheets collection is
null. I have tried running first ThisWorkbook_startup in ThisWorkbook object
(extended excel) and with and without excel running

Here is the code I have. See the notes that explain what is happening.
Please help.

Public Class Class1 : Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider
Public Function GetService(ByVal serviceType As System.Type) As
Object Implements
Microsoft.VisualStudio.Tools.Applications.Runtime. IRuntimeServiceProvider.GetService
Return Nothing
End Function

End Class
<TestMethod()> _
Public Sub FirstTest()
''this is as suggested in your email
Dim RuntimeCallback As
Global.Microsoft.VisualStudio.Tools.Applications.R untime.IRuntimeServiceProvider = New Class1 ''TODO: Initialize to an appropriate value

''this is written by visual studio. I changed the name of the
variable from target to extendedThisWorkbook. Easier for me to understand.
Dim extendedThisWorkbook As Global.ExcelTestWorkbook.ThisWorkbook =
New Global.ExcelTestWorkbook.ThisWorkbook(RuntimeCallb ack)

''my test starts here

''to run a method in a public module in the ExcelTestWorkbook project
Global.ExcelTestWorkbook.Main()

''TestThisFunction is a method in a public module in the
ExcelTestWorkbook project
''It will return true if Global.ExcelTestWorkbook.Main (previous
statement) works ok.
Dim actual As Boolean =
Global.ExcelTestWorkbook.Perro.TestThisFunction

''this does not fail, it is working fine. This means I have a
reference to a public module in the ExcelTestWorkbook Project working.
Assert.IsTrue(actual)

''this does not fail, it is working fine. This means I have a
reference to Thisworkbook (extended workbook) object in the
ExcelTestWorkbook Project.
Assert.IsTrue(extendedThisWorkbook.IAmAlive)

''here is where I have problems, any call like the one below throws
an exception
''system.nullReferenceException. extendedThisWorkbook is null.
''extendedThisWorkbook.InnerObject is null, same for
extendedThisWorkbook.Worksheets and similars
Assert.AreEqual(5, extendedThisWorkbook.Worksheets.Count)
End Sub


这里有一个限制。对于解决方法,我建议您在单元测试中创建自己的测试代码,例如,打开办公文档

(Excel工作簿),自动化和访问它的属性。


Luke Zhang

There is a limitation here. For a work around, I suggest you may create
your own test code in the unit test, for example, open the office document
(Excel workbook) with automation and access its properties.

Luke Zhang


这篇关于VSTO单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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