小编Old*_*Fox的帖子

Gallio.Model.ModelException:调用测试驱动程序时发生异常

Gallio.Model.ModelException:调用测试驱动程序时发生异常.---> System.Runtime.Serialization.SerializationException:在组件类型 'Microsoft.Cci.Pdb.PdbDebugException' '加利奥,版本= 3.4.0.0,文化=中性公钥= eb9cfa67ee6ab36e' 未标记为可序列.HResult:-2146233076

加载在Gallio.Icarus的visual studio 2015中开发的dll时遇到异常

.net c# mbunit unit-testing visual-studio-2015

5
推荐指数
1
解决办法
639
查看次数

Rhino 模拟存根两次相同的函数无法按预期工作

我最近开始使用Rhino,遇到了一种非常意想不到的行为,我无法克服。

问题是我有一个存根基础设施,在我的一项测试中,我需要更改预定义存根之一(在我的基础设施中)以返回与默认值不同的值。

我在以下代码中重现了该问题:

[TestFixture]
public class UnitTest1
{
    private IWorker _worker;

    [SetUp]
    void Setup()
    {
        _worker = MockRepository.GenerateStub<IWorker>();
        _worker.Stub(w=>w.DoWork()).Return(0);
    }

    [Test]
    public void DoWork_StubbingFunctionTwice_CallingTheLastStub()
    {
        int expected = 1;
        _worker.Stub(w => w.DoWork()).Return(expected);
        int actual =_worker.DoWork();
        Assert.AreEqual(expected, actual);
    }

}

public interface IWorker
{
    int DoWork();
}
Run Code Online (Sandbox Code Playgroud)

有人知道为什么犀牛存根会这样,更重要的是我怎样才能以最干净的方式解决它?

c# nunit unit-testing rhino-mocks mocking

5
推荐指数
1
解决办法
1559
查看次数

C#Rhino在第二次调用中使用硬编码参数模拟stubmethod

这可能不是甚至可能的事情,但我想我还是会问.反正我是否存在这个方法,以便使用我正在测试的方法中提供的参数来删除第二个调用?

存根的方法:

public SupportDetails GetSupportDetails(string languageKey)
{
    var result = FindSupportDetails(languageKey);

    return result ?? FindSupportDetails("en-us");
}
Run Code Online (Sandbox Code Playgroud)

我当前的测试:

public void GetsUSDetails_IfLangKeyDoesNotExist()
{
    var langKey = "it-it";

    _repo.Stub(s => s.FindSupportDetails(langKey))
         .Return(supportDetails.Where(sd => sd.LanguageKey == langKey)
                               .SingleOrDefault());

    ISupportRepository repo = _repo;
    var actual = repo.GetSupportDetails(langKey);

    Assert.AreEqual("en-us", actual.LanguageKey);
}
Run Code Online (Sandbox Code Playgroud)

以及测试中使用的supportDetails对象:

supportDetails = new SupportDetails[]
        {
            new SupportDetails()
            {
                ContactSupportDetailsID = 1,
                LanguageKey = "en-us"
            },
            new SupportDetails()
            {
                ContactSupportDetailsID = 2,
                LanguageKey = "en-gb"
            },
            new SupportDetails()
            {
                ContactSupportDetailsID = 3,
                LanguageKey = "es-es" …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing rhino-mocks mocking

5
推荐指数
1
解决办法
114
查看次数

如何使用 Nunit 和 RhinoMocks 模拟 HttpContext.Current.GetOwinContext()?

我关于 Mocking the HttpContext 的最后一个问题相比,我不得不将正在测试的方法更改为

public override void OnActionExecuting(HttpActionContext actionContext)
{
    HttpContext.Current.GetOwinContext().Set("RequestGUID", NewId.NextGuid());
    base.OnActionExecuting(actionContext);
}
Run Code Online (Sandbox Code Playgroud)

现在我需要弄清楚如何模拟 HttpContext.Current.GetOwinContext(),所以我可以为该Set()方法编写一个存根,或者通常能够测试这个特定的行。我怎样才能做到这一点?

c# unit-testing rhino-mocks mocking owin

5
推荐指数
1
解决办法
1793
查看次数

设置字符串参数时,对模拟的所有调用都必须具有相应的设置

我有一个简单的方法正在测试。运行测试时出现错误

“对模拟的所有调用都必须具有相应的设置”

在最后一行

dataField.DefaultValue = orderNumber.ToString();
Run Code Online (Sandbox Code Playgroud)

是什么原因造成的?

我只是在设定一个领域。

void IUtilities.SetOrderIdInDocumentMetaData(Document document, int orderNumber)
{
    DataField dataField = null;
    if (document.DataFields.IsPresent(ORDER_ID) == false)
    {
        dataField = document.DataFields.Add(ORDER_ID, AppDefault: false, DocDefault: false);
    }
    else
    {
        dataField = document.DataFields[ORDER_ID];
    }

    dataField.DefaultValue = orderNumber.ToString();
}
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试代码。

[TestMethod]
public void Utilities_SetOrderIdInDocumentMetaData_SetNew()
        {
    string orderNumber = "1";
    int orderId = 1;

    corelDocument
        .Setup(s => s.DataFields.IsPresent(ORDER_ID))
        .Returns(false);

    corelDocument
        .Setup(s => s.DataFields.Add(ORDER_ID, null, false, false, false))
        .Returns(corelDataField.Object);

    corelDataField
        .Setup(s => s.DefaultValue)
        .Returns(orderNumber);

    Utilities.SetOrderIdInDocumentMetaData(corelDocument.Object, orderId);

    Assert.AreEqual(orderNumber, corelDataField.Object.DefaultValue);
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest moq mocking

5
推荐指数
2
解决办法
2732
查看次数

Verify() 和 Setup()...VerifyAll() 之间的最小起订量差异

我正在创建几个单元测试,我想验证是否使用我期望的属性调用方法。

因此,鉴于这个非常简单的系统:

public class Employee
{
    public bool IsEmployed { get; set; }
}

public class DataStore
{
    public void UpdateEmployee(Employee obj)
    {
        // Save in DB
    }
}

public interface IDataStore
{
    void UpdateEmployee(Employee employee);
}

public Employee FireEmployee(IDataStore dataStore, Employee employee)
{
    employee.IsEmployed = false;

    dataStore.UpdateEmployee(employee);

    return employee;
}
Run Code Online (Sandbox Code Playgroud)

我想验证该DataStore.UpdateEmployee()方法是否在Employee.IsEmployed属性设置为 false时被调用。所以这里有两个测试用例,我认为它们应该完成同样的事情。

[Test]
public void TestViaVerify()
{
    //Arrange
    Mock<IDataStore> dataStore = new Mock<IDataStore>();
    var robert = new Employee { IsEmployed = true };

    //Act
    FireEmployee(dataStore.Object, …
Run Code Online (Sandbox Code Playgroud)

c# nunit unit-testing assert moq

5
推荐指数
1
解决办法
1508
查看次数

在模拟上预期调用一次,但是是2次:m => m.SaveChanges(),UnitTest

请我在测试此方法时遇到问题.

public class EFUrlRepository : IUrlsRepository
{
     public EFDbContext context = new EFDbContext();
     private Security security = new Security();
     public IQueryable<Url> Urls
     {
        get { return context.Urls; }
     }

     public bool AddUrl(Url url)
     {
          if(url.UrlId == 0)
          {
              context.Urls.Add(url);
              context.SaveChanges();
              url.UrlCode = security.Encrypt(url.UrlId.ToString());
              context.SaveChanges();
              return true;
          }
          return false;
     }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试上面类的addUrl.我试着像这里解释的那样实现

[TestMethod]
public void CreateUrl_saves_a_url_via_context()
{
    var mockSet = new Mock<DbSet<Url>>();
    var mockContext = new Mock<EFDbContext>();
    mockContext.Setup(m => m.Urls).Returns(mockSet.Object);

    var repository = new EFUrlRepository();
    Url url = …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq mocking

4
推荐指数
1
解决办法
1099
查看次数

MSTest Assert.AreEqual 因字符串数组而失败

我正在进行一些单元测试,但不确定为什么这个特定的测试失败了。

该测试是为了断言自定义视图引擎在正确的位置查找视图。

在我的自定义视图引擎中是这样的:

AreaMasterLocationFormats = new[]
{
    "~/Areas/{2}/App/{1}/Views/{0}.cshtml",
    "~/Areas/{2}/App/Shared/Views/{0}.cshtml"
};
Run Code Online (Sandbox Code Playgroud)

在我的测试中是这样的:

string[] expected = new[]
{
    "~/Areas/{2}/App/{1}/Views/{0}.cshtml",
    "~/Areas/{2}/App/Shared/Views/{0}.cshtml"
};

CustomRazorViewEngine engine = new CustomRazorViewEngine();

Assert.AreEqual(expected, engine.AreaMasterLocationFormats);
Run Code Online (Sandbox Code Playgroud)

测试失败并显示以下消息:

Message: Assert.AreEqual failed. Expected:<System.String[]>. Actual:<System.String[]>.
Run Code Online (Sandbox Code Playgroud)

(s/o 的引用格式不喜欢其中的第二个 lt...)

我不确定为什么,因为当我调试测试时一切看起来都很好。

c# unit-testing mstest

4
推荐指数
1
解决办法
1406
查看次数

Python单元测试中的Mocking数据库连接/未知类型

这里是蟒蛇新手.我的类使用数据库连接来包装一些函数.我已经成功找到了一些基本的例子.对于我正在使用的更复杂的库,我找不到模拟数据库连接的近似示例.在我的,

class DBSAccess():
    def __init__(self, db_con):
        self.db_con = db_con

    def get_db_perm(self, target_user):
      ## this is where I start having trouble
        with self.db_con.cursor() as cursor:
            cursor.execute("SELECT CAST(sum(maxperm) AS bigint) \
            FROM dbc.diskspace \
            WHERE  databasename = '%s' \
            GROUP BY databasename" % (target_user))

            res = cursor.fetchone()
            if res is not None:
                return res[0]
            else:
                msg = target_user + " does not exist"
                return msg
Run Code Online (Sandbox Code Playgroud)

其中db_con是teradata.UdaExec返回连接

udaExec = teradata.UdaExec (appName="whatever", version="1.0", logConsole=True)
db_con = udaExec.connect(method="odbc", system='my_sys', username='my_name', password='my_pswd')
dbc_instance = tdtestpy.DBSaccess (db_con) …
Run Code Online (Sandbox Code Playgroud)

python unit-testing mocking magicmock

4
推荐指数
1
解决办法
3192
查看次数

对于类似的自定义类列表,NUnit的CollectionAssert返回false

这是我的班级:

public class MyClass
{
    public string Name { get; set; }
    public string FaminlyName { get; set; }
    public int Phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有两个相似的列表:

List<MyClass> list1 = new List<MyClass>()
{
    new MyClass() {FaminlyName = "Smith", Name = "Arya", Phone = 0123},
    new MyClass() {FaminlyName = "Jahani", Name = "Shad", Phone = 0123}
};
List<MyClass> list2 = new List<MyClass>()
{
    new MyClass() {FaminlyName = "Smith", Name = "Arya", Phone = 0123},
    new MyClass() {FaminlyName = "Jahani", Name …
Run Code Online (Sandbox Code Playgroud)

c# nunit assert equality

3
推荐指数
1
解决办法
497
查看次数