我在通过Gallio运行集成测试时遇到问题。 当我使用Testdrive.NET或通过Visual Studio中的集成Gallio运行它时,测试工作正常。当我试图通过控制台运行它时(就像我们的nant脚本一样)它失败了。收到的消息是:
[失败]测试 TenForce.Execution.Api2.OData.Tests / AttachmentIntegrationTests / ATT achmentUpload执行 System.ServiceModel.CommunicationObjectFaultedException:The 通信对象,System.Data.Services.DataServiceHost,不能 用于通信,因为它处于Faulted状态。在 System.ServiceModel.Channels.CommunicationObject.Close(时间跨度 超时)at System.ServiceModel.ServiceHostBase.System.IDisposable.Dispose()at TenForce.Execution.Api2.OData.Tests.IntegrationTests.AttachmentIntegration Tests.AttachmentUpload()in d:\用户\ arne.de.herdt.TENFORCE2 \文档\ DEVELOPME NT \项目\罗宾逊\ TenForce.Execution.Api2.OData.Tests \ IntegrationTests \连接 mentIntegrationTests.cs:第83行
处置试验跑步者。停止时间:16:45(总执行时间: 20,515秒)
1次运行,0次通过,1次失败,0次不确定,0次跳过
完整的命令行如下:
Gallio.Echo.exe / r:IsolatedProcess TenFor ce.Execution.Api2.OData.Tests.dll /f:Namespace:TenForce.Execution.Api2.OData.Tes ts.IntegrationTests
我不知道在Gallio中导致这个问题的原因。它适用于VS,但不适用于构建代理或控制台。测试的源代码是:
using System.Data.Services;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace TenForce.Execution.Api2.OData.Tests.IntegrationTests
{
using System;
using System.Collections.Generic;
using System.ServiceModel.Web;
using MbUnit.Framework;
using Objects;
using Helpers;
using Test.Attributes;
/// <summary>
/// <para>This class contains all the integration tests to verify the correct working conditions for attachment entities.</para>
/// </summary>
public class AttachmentIntegrationTests : BaseIntegrationTest
{
/// <summary>
/// <para>This test will try to create a new attachment on an item using a local file.</para>
/// </summary>
[Test, MaxDuration]
public void AttachmentUpload()
{
#region Test Preparation
// Prepare a Workspace
var workspace = CreateWorkspaceObject();
Assert.IsTrue(Factory.CreateApi().Workspaces.Create(workspace), "Expected the test workspace to be created.");
// Prepare a List
var list = CreateList();
list.Workspace = workspace;
list.ItemType = new ItemType {Id = 5};
Assert.IsTrue(Factory.CreateApi().Lists.Create(list), "Expected the test list to be created.");
// Prepare an Item.
var itemFields = new List<ItemField>
{
new ItemField {FieldId = "SF19", Type = "List", ValueId = list.Id},
new ItemField {FieldId = "SF2", Type = "Title", Value = string.Format("I {0}", DateTime.Now)},
new ItemField {FieldId = "SF4", Type = "AssignedTo", ValueId = 1}
};
var item = new Item { ItemFields = itemFields.ToArray() };
Assert.IsTrue(Factory.CreateApi().Items.Create(item), "Expected the test item to be created.");
#endregion
using (var host = new DataServiceHost(typeof (Web.Api), new[] {BaseUri}))
{
// Start the host
host.Open();
// Create a new WebClient to create a call to the attachments resource
var client = new ODataClient {BaseUri = BaseUri, Username = "sadmin", Password = string.Empty};
// Send the file contents to the service using the correct url.
string response = client.UploadAttachment(GetTestFileLocation("ReportingTest.xls"), item.Id);
var parser = new ODataParser();
parser.LoadResponse(response);
// Fetch the Id of the Attachment, this should be greater than 0.
int attachmentId = parser.GetEntityId();
Assert.IsTrue(attachmentId > 0, "Expected the Id to be greater than zero.");
// Verify if the item is coupled to the correct Item.
response = client.GetResource(string.Format("Attachments({0})/Item", attachmentId));
parser.LoadResponse(response);
int itemId = parser.GetEntityId();
Assert.IsTrue(itemId == item.Id, "Expected the linked item to have a matching Id.");
// Change the filename of the uploaded file and verify whether the file is properly renamed.
client.UpdateProperty(string.Format("Items({0})/Attachments({1})/Filename/$value", itemId, attachmentId), "uploaded_excel.xls");
// Verify if the changes made it to the database.
Attachment att = Factory.CreateApi().Attachments.Read(attachmentId);
Assert.AreEqual("uploaded_excel.xls", att.Filename, "Expected the data to be changed on the entity.");
Assert.IsTrue(System.IO.File.Exists(Factory.CreateApi().Attachments.GetAttachmentPath(att, false)), "Expected the file to be present on the hard drive.");
// Close the host properly
host.Close();
}
}
}
}
我是否遗漏了在单元测试中托管DataService的事情?
编辑1 运行以下命令:
netsh http add urlacl url = http:// +:60000 / ODataService / 用户=管理员
解决了部分问题。我现在可以通过控制台在我的开发系统上运行测试而没有问题,但是构建代理仍然无法运行测试。他们推动以下输出:
失败执行System.Net.WebException:远程服务器返回了一个 错误:(500)内部服务器错误。状态:ProtocolError响应: System.Net.WebClient.UploadFile上的System.Net.HttpWebResponse(Uri 地址,字符串方法,字符串fileName)at System.Net.WebClient.UploadFile(Uri地址,String fileName)at System.Net.WebClient.UploadFile(String address,String fileName)at TenForce.Execution.Api2.OData.Tests.Helpers.ODataClient.UploadAttachment(字符串 path,Int32 itemId)in C:\罗宾逊\树干\项目\罗宾逊\ TenForce.Execution.Api2.OData.Tests \助手\ ODataClient.cs:线 69点 TenForce.Execution.Api2.OData.Tests.IntegrationTests.AttachmentIntegrationTests.AttachmentUpload() 在 C:\罗宾逊\树干\项目\罗宾逊\ TenForce.Execution.Api2.OData.Tests \ IntegrationTests \ AttachmentIntegrationTests.cs:线 89 -------标准输出:-------无法读取配置部分 通用/记录。使用无操作实现。
答案 0 :(得分:0)
我花了很长时间来弄清楚这一点,但问题不在于加利奥。 问题是用于开发OData服务的工具包。此工具包无法从控制台托管环境运行。
将服务移动到远程服务器并编写测试以远程调用函数并解析响应后,我们得到了预期的行为。