我们将FxCop用于所有项目.对于我们的UnitTests,我不确定它是否值得.我们最终得到了许多抑制:
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = SuppressJustifications.CA1822MethodIsUsedExternallyAsNonStatic)]
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Cantaloupe.Seed.Security.RijndaelEncryption", Justification = SuppressJustifications.CA1806MethodIsCalledForExceptionThrowingTest)]
Run Code Online (Sandbox Code Playgroud)
人们对单元测试代码的FxCop有什么看法?
由于fxcop是一个程序集检查器,是否可以创建一个自定义规则来检查每个文件是否有多个类?
如果是的话,怎么做?
谢谢你的帮助
我们目前正在使用TFS 2008进行源代码控制和持续集成.
我们使用FXCop检查检查性能和安全警告.建筑师或高级开发人员在冲刺结束时或交付前运行FX Cop.
我们希望这个作为CI的一部分运行,如果有警告就失败,那么最好的方法是什么?
我的构建配置中有两个构建步骤:Visual Studio(sln)构建运行器,然后是FxCop构建运行器.我正在使用TeamCity 6.0.1和FxCop 10.0.
开箱即用,TeamCity中的FxCop运行器似乎只报告规则违规,并在代码检查选项卡上生成报告.我想确保如果发生任何违规,构建步骤失败,从而导致整个构建失败.
有没有办法实现这个目标?
我正在使用C#开发项目。现在,我已经设置了Hudson服务器,并且在服务器上安装了.Net 4 Framework和Windows SDK 7.1。
该项目成功构建,但是当我使用以下命令启动FxCop时:
"C:\Program Files (x86)\Microsoft FxCop 1.35\fxcopcmd.exe" /file:CommonServiceTool\bin\Release /out:fxcop-result.xml
Run Code Online (Sandbox Code Playgroud)
它加载所有.dll规则文件,然后发生以下异常:
Using system files at: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319.
Could not resolve reference to PresentationFramework.
Could not resolve reference to PresentationCore.
Could not resolve reference to WindowsBase.
12 exceptions occurred while loading Project1.
00) Could not resolve type reference: [PresentationFramework]System.Windows.StartupEventArgs.
01) Could not resolve type reference: [PresentationFramework]System.Windows.Window.
02) Could not resolve type reference: [PresentationFramework]System.Windows.Controls.HeaderedContentControl.
03) Could not resolve type reference: [PresentationCore]System.Windows.Input.ICommand.
04) Could not resolve member reference: System.Windows.ThemeInfoAttribute::.ctor.
... and …Run Code Online (Sandbox Code Playgroud) 我有一个声明事件的接口
interface IMyInterface
{
event SomeHandler MyEvent1;
event SomeHandler MyEvent2;
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我启用代码分析规则CA1040:避免空接口时,它会抱怨我的界面违反了这个规则,任何想法如何解决它?
我有一个使用Autofac和WebAPI集成的项目
我们在我们的解决方案上运行FxCop,在打开autofac时,我得到以下结果:
找不到以下间接引用的程序集.分析不需要此装配,但没有它,分析结果可能不完整.这个汇编由Autofac.dll引用
System.Core,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e,Retargetable = Yes
这会导致CI服务器出现以下错误:
FxCop返回码(516):PROJECT_LOAD_ERROR ASSEMBLY_REFERENCES_ERROR
有没有办法告诉FxCop忽略这个?
最近,我一直在努力在项目中启用更多的Visual Studio代码分析规则。但是,我一直碰到规则CA2227:“集合属性应该是只读的”。
说我有一个像这样的模型类:
public class Foo
{
public string Name { get; set; }
public List<string> Values { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个映射很深的视图页面:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
for (int i = 0; i < Model.Values.Count; i++)
{
<br />
@Html.LabelFor(m => Model.Values[i]);
@Html.EditorFor(m => Model.Values[i]);
}
<button type="submit">Submit</button>
}
Run Code Online (Sandbox Code Playgroud)
使用ASP.NET MVC,我可以在控制器中编写一个动作,该动作将自动将该输入绑定到type类型的类Foo:
[HttpPost]
public ActionResult ProcessForm(Foo model)
{
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,我的List<string>自动属性违反了规则CA2227。如果不进行模型绑定,则可以使该属性为只读,并在其他位置填充该集合。但是,该方法不适用于默认的模型绑定器。目前,我只是在视图模型中添加抑制消息。
有没有一种方法可以在不违反CA2227的情况下绑定模型中的项目集合?还是在这里添加抑制消息是我最好的选择?
我有很多像这样的测试类.
[TestClass]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
public class TestClass
{
private IDisposable _disposable;
[TestInitialize]
public void TestInitialize()
{
_disposable = //new disposable object...;
}
[TestCleanup]
public void TestCleanup()
{
_disposable.Dispose();
}
[TestMethod]
public void Test1()
{
//Uses _disposable
}
[TestMethod]
public void Test2()
{
//Uses _disposable
}
[TestMethod]
public void TestN()
{
//Uses _disposable
}
}
Run Code Online (Sandbox Code Playgroud)
使用FxCop进行静态分析会导致以下警告,因为我没有在我的测试类上实现dispose模式.
"CA1001:拥有一次性田地的类型应该是一次性的"
现在我只是在源代码中抑制消息,但我觉得必须有一个更好的方法,而不是使用SuppressMessageAttribute来混乱我的所有测试.这似乎是测试中的常见模式 - 为测试创建对象,然后在测试后将其配置.我不能在测试类上实现IDisposable,因为只为所有测试方法创建了一个测试对象.我想在每个测试方法之间处理这个对象.
我知道我可以在每个测试中创建对象并将其配置在测试中,但我宁愿继续使用SuppressMessageAttribute复制并将相同的代码粘贴到每个测试方法中.这似乎是两个邪恶中较小的一个.是否有更好的方法在每次测试之前创建一次性对象,并在每次测试后处理它而不会导致警告CA1001?
我在这里先向您的帮助表示感谢.
在一些地方,人们建议使用private void Dispose(bool)该IDisposable模式.这似乎过时了(至少对于未密封的类),因为新建议的模式(根据微软)protected virtual void Dispose(bool).
The thing is, Code Analysis does not report private void Dispose(bool) for violating CA1063, even though it seems to violate the pattern directly.
What's up with this? Is private void Dispose(bool) somehow getting called (or compiled to something that looks like protected virtual Dispose(bool)?
If this is some issue with Code Analysis and is the incorrect pattern, are there ways to detect this? Possibly with StyleCop? …
fxcop ×10
c# ×6
unit-testing ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
autofac ×1
dispose ×1
fxcopcmd ×1
hudson ×1
idisposable ×1
teamcity ×1
tfs ×1
tfs2008 ×1