【发布时间】:2011-01-13 16:50:07
【问题描述】:
我对单元测试和模拟是全新的,并且仍然在耳后湿漉漉。我正在使用 Moq 框架,我需要模拟一个集合,以便它产生一个具有我提供的值的成员。
有问题的集合类是System.Configuration.SettingsPropertyCollection,其中包含SettingsProperty 对象。
反过来,SettingsProperty 有一个 Attributes 属性,该属性返回一个 SettingsAttributeDictionary。
我需要我的集合生成一个SettingsProperty,它在其Attributes.SettingsAttributeDictionary 中有一个自定义属性(派生自System.Attribute)。
我真的很难解决这个问题,但到目前为止无济于事。我试过伸出我的舌头并用它做鬼脸,但没有任何效果。
这是我到目前为止尝试过的代码,在代码中注释的地方抛出了一个异常,所以当然测试总是失败。
[TestMethod]
public void GetPropertySettings_Should_Return_Default_Values_When_Device_Not_Registered()
{
const string deviceName = "My.UnitTest";
const string deviceType = "Switch";
var deviceId = String.Format("{0}.{1}", deviceName, deviceType);
Mock<IProfile> mockProfile = new Mock<IProfile>();
Mock<SettingsContext> mockSettingsContext = new Mock<SettingsContext>();
// Construct a SettingsPropertyCollection populated with a single property.
// The single property will have a fixed name and default value, and will also have a single
// attribute, giving teh ASCOM DeviceId.
var deviceAttribute = new ASCOM.DeviceIdAttribute(deviceId);
var attributes = new SettingsAttributeDictionary();
attributes.Add(typeof(DeviceIdAttribute), deviceAttribute);
var settingsProperty = new SettingsProperty(SettingName, typeof(string), null, false, SettingDefaultValue, SettingsSerializeAs.String, attributes, true, true);
var propertyCollection = new SettingsPropertyCollection();
propertyCollection.Add(settingsProperty);
// Now comes the interesting part where we call our IProfile - this is where we really need Moq.
// Expectations:
// - mockProfile must have it's DeviceType set.
// - mockProfile's device type (captured in setDeviceType) must match deviceType.
// - The returned SettingsPropertyValueCollection must not be empty.
// - The returned SettingsPropertyValueCollection must have exactly one entry.
// - The entry must match the value of SettingDefaultValue.
// Expectation: IProfile must have its DeviceType set. We capture the value into setDeviceType.
var setDeviceType = String.Empty;
mockProfile.SetupSet(x => x.DeviceType).Callback(y => setDeviceType = y);
// Finally, it is time to call the method we want to test
var settingsProvider = new SettingsProvider(mockProfile.Object);
// THE NEXT LINE THROWS AN EXCEPTION
// IF I TRY TO STEP INTO IT, IT NEVER RETURNS AND THE TEST RUN JUST ENDS.
var result = settingsProvider.GetPropertyValues(mockSettingsContext.Object, propertyCollection);
// Now lets verify that everything went as expected
// First, let's test that the parsing of DeviceId was as expected: IProvider.DeviceType was set to the expected value
Assert.AreEqual(deviceType, setDeviceType);
// Then let's test that the methods of IProvider that we mocked were called
mockProfile.VerifyAll();
// With this done, let's turn to the output of the method
// Firstly, we test that the resulting collection contains exactly one item of the type SettingsPropertyValue
Assert.IsTrue(result.Count > 0);
Assert.AreEqual(1, result.Count);
Assert.IsTrue(result.OfType<SettingsPropertyValue>().Count() > 0);
// Then let's inspect the contained SettingsProviderValue further
var settingsPropertyValue = result.OfType<SettingsPropertyValue>().First();
// First IsDirty flag must never be set
Assert.IsFalse(settingsPropertyValue.IsDirty);
// The PropertyValue must be the default value we passed in
Assert.AreEqual(SettingDefaultValue, settingsPropertyValue.PropertyValue);
}
抛出的异常(由测试运行程序报告)是:
测试方法 ASCOM.Platform.Test.SettingsProviderTest.GetPropertySettings_Should_Return_Default_Values_When_Device_Not_Registered 抛出异常:System.ArgumentException:System.Configuration.SettingsContext 类型实现 ISerializable,但未能提供反序列化构造函数。
【问题讨论】:
-
包含异常的详细信息总是有帮助的
-
@AdamRalph 道歉。问题已更新,包含异常的详细信息。
-
我不明白您的代码如何编译... SettingsProvider 是一个具有默认构造函数的抽象类,所以我看不出如何使用 IProfile 实例“新建”它。
-
你看到的类是一个派生类(一个自定义的 SettingsProvider),我在其中添加了一个构造函数,可以让我进行依赖注入。也许我应该在我的代码中完全限定命名空间以使其显而易见,但由于它是被测单元,我认为这可能很清楚。
标签: c# unit-testing mocking moq