English 中文(简体)
PexAssert。 PUT与Run Pex合并
原标题:PexAssert.Throws in PUT in combination with Run Pex

当我创建PUT时,“Run Pex”将使用这一方法,并为之进行自动生成测试。

只要对这种方法的号召直接在中央直辖区内进行,这只能奏效。

样本:

The PUT is used by “Run Pex”:

[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered<T>(
    [PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
    var result = "";
    result = target.Convert(objectToConvert);
    return result;
}

The PUT is not used by “Run Pex”:

[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered_ThrowsInvalidOperationException<T>(
    [PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
    var result = "";
    PexAssert.Throws<InvalidOperationException>(() => target.Convert(objectToConvert));
    return result;
}

我想表明,要求采用这种方法总是放弃这一例外,而不考虑参数。

如何做到这一点?

我已经询问,一周前,在

这个问题可如下文所示:

  1. Have a simple solution with one project with one class with one method.
  2. Right click on that method and choose Run Pex.
  3. In the Pex Exploration Results right click on any entry and choose "Save Test...". It will create a new test project with two relevant files: <class-name>Test.cs and <class-name>Test.<MethodName>.g.cs. In the first file, there is the PUT, in the second files are the specific test methods, one for each parameter Pex chooses. The second file is automatically recreated every time you run pex for a method that has a PUT in the first file.
  4. Right click on the test project and choose "Pex" -> "Delete generated unit tests". This will remove the specific tests from the second file.
  5. Go to the file with the PUT and rename the PUT to an arbitrary name.
  6. Go back to the method from point 1 and again right-click and choose Run Pex.
  7. Pex will create a new file in the test project named `Test..g.cs. It uses the same PUT as before, although you renamed it.
  8. Change the PUT again by leaving the name as it is but changing the content to my second example with PexAssert.Throws.
  9. Again, right click on the test project and choose "Pex" -> "Delete generated unit tests".
  10. Again, right click in the method from point 1 and choose "Run Pex"
  11. Open both of the *.g.cs files in the test project and you will see, that there are no tests. This means, Pex didn t use the PUT. That s the problem :)
问题回答

我刚刚尝试在2010年视力演播室举办两个例子。 我改变了第二个例子,使用一个称为<代码>ThrowingConverter的类别,这些类别是我的讽刺执行情况:

public class ToStringConverter {
    internal string Convert(object objectToConvert) {
        return objectToConvert.ToString();
    }
}

public class ThrowingConverter {
    internal string Convert(object objectToConvert) {
        if (objectToConvert is string) {
            return (string)objectToConvert;
        }
        throw new InvalidOperationException();
    }
}

我跑了Pex,并提出了以下测试案例:

  • ToStringConverter:
    • int
      • 0
      • 1
    • string
      • null (fails with NullReferenceException)
      • ""
    • object
      • null (fails with NullReferenceException)
      • new object()
  • ThrowingConverter:
    • int
      • 0
    • string
      • null
      • "" (fails because it returns "")
    • object
      • null
      • "" (fails because it returns "")

This is exactly what I expected it to find, so I don t see your problem really.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签