我将一个实例存储在一个纤细的符号 ($IT) 中,但是当我稍后尝试在函数调用中使用该实例时,我收到一个 fitSharp.Machine.Exception.ParseException。
我认为问题在于 FitSharp 尝试解析参数,而不是将对象投射到其接口。
我有以下类和接口(命名空间是 MySlimTest)
public interface IObject {}
public class ConcreteObject: IObject
{
public string Name { get; set; }
public string Description { get; set; }
}
我在我的苗条夹具中使用它,其中包括以下方法和我存储在苗条符号中的返回实例。
public IObject CreateConcreteObjectWithNameAndDescription(string name, string description)
{
return new ConcreteObject() {Name = name, Description = description};
}
我从脚本表中调用此方法。在测试运行后呈现时,它看起来像这样:
$IT<-[MySlimTest.ConcreteObject] | create concrete object | with name | The initial name of the concrete object | and description | empty |
当我稍后尝试使用存储在纤细符号 IT 中的实例时,会引发 ParseException。
夹具中的方法是
public bool SetNameForInstance(string name, IObject obj)
{
return SetAttribute<ConcreteObject>(obj, concreteObject => concreteObject.Name = name);
}
这在测试表中用作
ensure | set name | John Cleese | for instance | $IT->[MySlimTest.ConcreteObject]
解决方法
有趣的是,如果我将方法签名更改为使用实现(ConcreteObject)而不是接口,那么它就可以工作。
public bool SetNameForInstance(string name, ConcreteObject obj)
带有fixture 代码的完整示例如下作为fitnesse 纯文本wiki 页面:
!*> Environment
Linux 3.2.0-3-amd64 #1 SMP Mon Jul 23 02:45:17 UTC 2012 x86_64 GNU/Linux
Mono 2.10.8.1 (Debian 2.10.8.1-7) (64-bit)
glib-sharp 2.12.0.0
FitNesse (v20121220)
FitSharp release 2.2 for .net 4.0
*!
!*> Fixture Code
{{{
using System;
namespace MySlimTest
{
public interface IObject {}
public class ConcreteObject: IObject
{
public string Name { get; set; }
public string Description { get; set; }
}
public class SymbolTestFixture
{
public SymbolTestFixture ()
{
// Initialisation code here...
// Currently just a mock.
}
public IObject CreateConcreteObjectWithNameAndDescription(string name, string description)
{
return new ConcreteObject() {Name = name, Description = description};
}
public bool SetNameForInstance(string name, IObject obj)
{
return SetAttribute<ConcreteObject>(obj, concreteObject => concreteObject.Name = name);
}
public bool SetDescriptionForInstance(string description, ConcreteObject obj)
{
return SetAttribute<ConcreteObject>(obj, concreteObject => concreteObject.Description = description);
}
private bool SetAttribute<T>(IObject obj, Action<T> fun)
{
if (obj is T)
{
fun((T)obj);
return true;
}
return false;
}
}
}
}}}
*!
!3 Set-up of the test system
!***> Test system set-up
!define TEST_SYSTEM {slim}
!path /path/to/fixture/DLL/SlimTest.dll
!define COMMAND_PATTERN {%m -r fitSharp.Slim.Service.Runner,/path/to/fitSharp.dll %p}
!define TEST_RUNNER {/path/to/Runner.exe}
***!
!**> Usings
!|import |
|MySlimTest|
**!
!2 Scenarios
!|scenario|given concrete object |name |and |description |
|$IT= |create concrete object with name|@name|and description|@description|
!|scenario |given concrete object|name |
|given concrete object|@name |and|empty|
!|scenario|edit concrete object|instance |name to |name|and description to|description|
|ensure |set name |@name |for instance|$IT |
|ensure |set description |@description|for instance|$@instance |
!3 Test the ''slim symbol can hold object as parameter'' feature
!|script|symbol test fixture|
!|script |
|given concrete object|The initial name of the concrete object |
|edit concrete object |IT |name to |John Cleese |and description to|Yes, I am still indeed alive, contrary to rumour, and I am touring my one Cleese show. London.|