不确定这是一个已知问题.我正在使用VS2012 RC(Ultimate)和Win8 Release Preview.我创建了一个单元测试库(metro style app),并编写了一个单元测试,其中包括async/await关键字.但是,当我编译单元测试项目时,单元测试资源管理器没有显示我写的测试.如果我排除了async/await关键字,那么单元测试资源管理器会出现在我刚写的测试中.有没有人以前遇到过这个,还是仅仅是我?
[TestClass]
public class UnitTest1
{
[TestMethod]
public async void SomeAsyncTest()
{
var result = await StorageFile.GetFileFromPathAsync("some file path");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用xUnit,SubSpec和FakeItEasy进行单元测试.到目前为止,我已经创建了一些积极的单元测试,如下所示:
"Given a Options presenter"
.Context(() =>
presenter = new OptionsPresenter(view,
A<IOptionsModel>.Ignored,
service));
"with the Initialize method called to retrieve the option values"
.Do(() =>
presenter.Initialize());
"expect the view not to be null"
.Observation(() =>
Assert.NotNull(view));
"expect the view AutoSave property to be true"
.Observation(() => Assert.True(view.AutoSave));
Run Code Online (Sandbox Code Playgroud)
但是现在我想写一些负面单元测试并检查某些方法是否被调用,并抛出异常
例如
"Given a Options presenter"
.Context(() =>
presenter = new OptionsPresenter(view,
A<IOptionsModel>.Ignored,
service));
"with the Save method called to save the option values"
.Do(() =>
presenter.Save());
"expect an ValidationException to be thrown" …Run Code Online (Sandbox Code Playgroud) 在C#6之前,属性的初始化不使用支持字段来初始化默认值.在C#6中,它使用支持字段来初始化新的自动初始化属性.
我很好奇为什么在C#6 IL之前使用属性定义进行初始化.这有什么特别的原因吗?或者在C#6之前没有正确实施?
在C#6.0之前
public class PropertyInitialization
{
public string First { get; set; }
public string Last { get; set; }
public PropertyInitialization()
{
this.First = "Adam";
this.Last = "Smith";
}
}
Run Code Online (Sandbox Code Playgroud)
编译器生成代码(IL表示)
public class PropertyInitialisation
{
[CompilerGenerated]
private string \u003CFirst\u003Ek__BackingField;
[CompilerGenerated]
private string \u003CLast\u003Ek__BackingField;
public string First
{
get
{
return this.\u003CFirst\u003Ek__BackingField;
}
set
{
this.\u003CFirst\u003Ek__BackingField = value;
}
}
public string Last
{
get
{
return this.\u003CLast\u003Ek__BackingField;
}
set
{
this.\u003CLast\u003Ek__BackingField = value;
}
} …Run Code Online (Sandbox Code Playgroud) 我最近开始使用AutoFixture库(http://autofixture.codeplex.com/)进行单元测试,我非常喜欢它.
我从AutoFixture CodePlex网站获得了此代码示例.我的问题是关于8号线.
1. [TestMethod]
2. public void IntroductoryTest()
3. {
4. // Fixture setup
5. Fixture fixture = new Fixture();
6.
7. int expectedNumber = fixture.CreateAnonymous<int>();
8. MyClass sut = fixture.CreateAnonymous<MyClass>();
9.
10. // Exercise system
11. int result = sut.Echo(expectedNumber);
12.
13. // Verify outcome
14. Assert.AreEqual<int>(expectedNumber, result, "Echo");
15. // Teardown
16. }
Run Code Online (Sandbox Code Playgroud)
我无法理解,为什么我们需要创建一个被测试类的匿名对象.
MyClass sut = fixture.CreateAnonymous<MyClass>();
Run Code Online (Sandbox Code Playgroud)
该类应该是IMO的真实对象.举个例子..
var sut = new MyClass();
Run Code Online (Sandbox Code Playgroud)
我的问题是,创建一个匿名对象进行测试的真正好处是什么?
我在ASP.NET MVC 5项目中下载了Mvc4Future nuget包.当我使用ActionFilter属性如[AjaxOnly]时会导致"类型违反的继承安全规则"异常.这是完整的信息:
违反类型的继承安全规则:'Microsoft.Web.Mvc.AjaxOnlyAttribute'.派生类型必须与基本类型的安全可访问性匹配,或者不太容易访问.
我该如何解决这个问题?
我想知道有没有人遇到过这个问题,其中MSTest单元测试没有出现在新的单元测试资源管理器中.
我正在运行Windows 7(32位).我从下面的链接下载了VS11开发者预览版.http://www.microsoft.com/download/en/details.aspx?id=27543
我创建了一个示例C#Console应用程序,并从MSTest项目模板添加了测试库.然后我创建了一个示例单元测试,并重建解决方案.当我打开测试资源管理器(View-> OtherWindows-> UnitTest Explorer)时,我没有看到任何测试加载.
我只看到一条消息说......"没有发现任何测试.请构建您的项目并确保安装了适当的测试框架适配器".
我假设自动安装了MSTest适配器.如果不是,我甚至不确定如何安装适配器.
我可能会在这里遗漏一些东西,但我无法弄明白.有没有人遇到过这个问题?
下面是一个示例实现,它使用metro API和数据绑定(使用MVVM)填充下拉列表中的文件夹列表.
View模型的构造函数使用SetFolders方法(private async),它调用一个等待的方法fileService.GetFoldersAsync()来获取文件夹列表.然后将文件夹列表分配给名为"FoldersList"的属性.XAML使用此属性使用数据绑定填充下拉列表.
我想知道是否有更好的方法来设置FoldersList属性,而不必在构造函数中设置它,如下所示.我希望调用GetFilesAsync方法并在发生实际数据绑定时设置FilesList属性值(不是在init类中).由于属性不支持async/await修饰符(据我所知),我正在努力实现一个合适的解决方案.任何想法都非常感激.
代码如下.
视图模型
public class FileViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly IFileService fileService;
public FileDataViewModel(IFileService fileService)
{
this.fileService = fileService;
SetFolders();
}
private async void SetFolders ()
{
FoldersList = await fileService.GetFoldersAsync();
}
private IEnumerable< IStorageFolder > foldersList;
public IEnumerable<StorageFolder> FoldersList
{
get { return foldersList; }
private set
{
foldersList = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FoldersList"));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
IFileService和实现
public interface IFileService {
Task<IEnumerable<IStorageFolder>> GetFilesAsync(); …Run Code Online (Sandbox Code Playgroud) asynchronous async-await c#-4.0 microsoft-metro windows-runtime
最初的问题是: 使用AutoFixture对Html Helper进行单元测试
不确定我是否应该重新打开原来的问题; 但是,由于我将原始问题标记为已解决,因此我决定创建一个新问题.如果我做错了,请道歉.
我正在使用Mark建议的方法,但在使用Freeze时我遇到了困难.
以下是完整的源代码...... Class Under Test:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcDemo2.Helpers
{
public static class Keys
{
public static readonly string SomeKey = "SomeKey";
}
public static class SampleHelpers
{
public static MvcHtmlString SampleTable(this HtmlHelper helper,
SampleModel model, IDictionary<string, object> htmlAttributes)
{
if (helper == null)
{
throw new ArgumentNullException("helper");
}
if (model == null)
{
throw new ArgumentNullException("model");
}
TagBuilder tagBuilder = new TagBuilder("table");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.GenerateId(helper.ViewContext.
HttpContext.Items[Keys.SomeKey].ToString());
return …Run Code Online (Sandbox Code Playgroud) 我有一个简单的metro应用程序包含一个按钮,一个标签和一个下拉列表.下拉列表包含我可以读取的文件列表.当我单击按钮时,所选文件的内容将被读入标签.文件位于Documents文件夹中(即WinRT的KnownFolders.DocumentsLibrary).每个文件代表WinRT API中的StorageFile.
文件读取方法是一种异步方法(使用async/await).为了证明异步行为,我将文件读取方法作为一个长时间运行的过程.因此,在执行这个长时间运行的方法时,我应该能够自由地单击下拉列表并选择一个不同的文件.这是因为在读取文件时不应阻止UI线程.然而,目前还没有发生这种情况.它似乎仍然阻止UI线程,并且在长时间运行的方法发生时下拉列表被冻结.我必须在这里做一些奇怪的事.你能告诉我为什么用户界面没有响应吗?我的代码示例如下.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace FileAccess
{
public sealed partial class MainPage : Page
{
private readonly StorageFolder storageFolder;
public MainPage()
{
this.InitializeComponent();
storageFolder = KnownFolders.DocumentsLibrary;
AddFiles();
}
private async void AddFiles() //Add files to the drop down list
{
var filesList = await storageFolder.GetFilesAsync();
IEnumerable<FileItem> fileItems
= filesList.Select(x => new FileItem(x.Name, x.Name));
cboSelectFile.ItemsSource = fileItems;
cboSelectFile.SelectedIndex = 0;
}
private async void …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AutoFixture对Html Helper进行单元测试.以下是我的SUT
public static MvcHtmlString SampleTable(this HtmlHelper helper,
SampleModel model, IDictionary<string, object> htmlAttributes)
{
if (helper == null)
{
throw new ArgumentNullException("helper");
}
if (model == null)
{
throw new ArgumentNullException("model");
}
TagBuilder tagBuilder = new TagBuilder("table");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.GenerateId(helper.ViewContext.HttpContext.Items[Keys.SomeKey].ToString());
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它只返回一个带有表标签的MVC Html字符串,并附加了Id.(参见下面的单元测试结果示例)
使用AutoFixture进行单元测试:
[Fact]
public void SampleTableHtmlHelper_WhenKeyExistWithinHttpContext_ReturnsExpectedHtml()
{
var fixture = new Fixture();
//Arrange
fixture.Inject<HttpContextBase>(new FakeHttpContext());
var httpContext = fixture.CreateAnonymous<HttpContextBase>();
fixture.Inject<ViewContext>(new ViewContext());
var vc = fixture.CreateAnonymous<ViewContext>();
vc.HttpContext = httpContext;
vc.HttpContext.Items.Add(Keys.SomeKey, "foo");
fixture.Inject<IViewDataContainer>(new FakeViewDataContainer());
var htmlHelper = fixture.CreateAnonymous<HtmlHelper>();
var …Run Code Online (Sandbox Code Playgroud) c# ×5
unit-testing ×4
autofixture ×3
async-await ×2
asynchronous ×2
c#-4.0 ×2
.net ×1
asp.net-mvc ×1
async-ctp ×1
bdd ×1
c#-5.0 ×1
c#-6.0 ×1
fakeiteasy ×1
mstest ×1
xunit ×1