我有一个课我需要单元测试.
对于我正在开发c#并使用NUnit的背景,但我的问题更具理论性:
我不知道我是否已经编写了足够的测试方法,如果我检查了所有方案.是否有已知的工作方法/最佳实践/规则集合?
就像是
(这是一个可能的规则的愚蠢的例子,但如果我有一些不傻的东西,我不会问这个问题)
我有一个加载外部DLL的MVC应用程序,在生产中我根本没有错误.Firefox只是说连接已重置.所以我在代码中放了一些try/catch但它们仍然无法工作,我仍然得到连接重置消息.
我知道错误是BadImageFormatException,但为什么我在浏览器中看不到任何内容?
public class HomeController : Controller
{
[DllImport("CDCrypt.dll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String Encrypt([MarshalAs(UnmanagedType.LPStr)] String aName);
[DllImport("CDCrypt.dll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String Decrypt([MarshalAs(UnmanagedType.LPStr)] String aName);
//
// GET: /Home/
public ActionResult Index()
{
try
{
ViewBag.EncryptString = Encrypt("test");
}
catch (Exception e)
{
ViewBag.EncryptString = "Stack Trace\r\n:" + "\r\nException: " + e.Message;
return new HttpStatusCodeResult(500);
}
return View();
}
public ActionResult Up()
{
ViewBag.Up = "You can see me";
return View();
}
}
Run Code Online (Sandbox Code Playgroud) 我最近开始从事一个项目,我想尝试行为驱动开发。我选择 SpecFlow 作为我的测试框架,因为它是一个 C# 应用程序。根据我在网上阅读的有关该主题的内容,BDD 相对于 TDD 的一个优点是 BDD 可用于测试用户界面。但是,我在使用 SpecFlow 执行此操作时遇到了一些麻烦。对于我的第一次测试,我想编写以下功能:
用户单击一个按钮,会出现一个文件夹浏览对话框,选择一个文件夹后,应用程序会在该文件夹中搜索与应用程序相关的文件,并将这些文件加载到应用程序中。
下面的场景或多或少是我想写的:
Scenario: Add Model
Given I am on the main screen
When I click Add Model
And select a folder
And the folder contains a model
Then the model is loaded into the application
Run Code Online (Sandbox Code Playgroud)
我需要什么代码才能让 SpecFlow 单击该 FolderBrowseDialog 上的内容才能完成此测试?
我目前正在尝试使用"Effort"框架(http://effort.codeplex.com/wikipage?title=Tutorials&referringTitle=Home)对实体框架的上下文类进行单元测试
如果我的单元测试项目有两个包含方法的类使用努力,那么我得到以下错误:
参数'xmlReader'无效.必须至少提供一个.ssdl工件.
看起来,使用多个方法在多个类中使用工作会导致错误.我宁愿不在一个班级中拥有所有的单元测试功能.
代码测试正在运行:
IDataLoader loader = new Effort.DataLoaders.CsvDataLoader(Path.Combine(TestContext.DeploymentDirectory, "csvFiles"));
using (EntityConnection connection = Effort.EntityConnectionFactory.CreateTransient("name=Entities", loader))
{
BussinnesLayer.Customer[] customers = Customer.GetCustomers(connection);
Assert.IsTrue(customers.Length > 0, "Customer list length = 0");
}
Run Code Online (Sandbox Code Playgroud)
App.Config包含以下实体连接字符串:(删除敏感数据)
<add name="Entities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="data source=servername;initial catalog=database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
我有一个用户控件,它在我想测试的ValidateChildren方法中做了一些验证.我创建了一个用户控件的部分模拟,但是虽然我没有对ValidateChildren方法设置任何期望,但我只是调用它,它只是被跳过而且方法中的代码永远不会执行.为了尝试了解发生了什么,我创建了一个简单的测试,如下所示:
public class Foo
{
public virtual bool Method1()
{
throw new NotImplementedException();
}
public virtual bool Method2()
{
return Method1();
}
}
Run Code Online (Sandbox Code Playgroud)
并用它来测试它:
[Test]
public void TestFooMethods ()
{
MockRepository m = new MockRepository();
Foo foo = m.PartialMock<Foo>();
RhinoMocksExtensions.Expect<Foo,bool>(
foo,
delegate (Foo obj)
{
return obj.Method1();
}
).Return(true);
Assert.IsTrue (foo.Method2());
}
Run Code Online (Sandbox Code Playgroud)
现在我希望foo.Method1被嘲笑和foo.Method2不是.但是这总是返回false,如果我尝试在调试器中逐步执行foo.Method2(),我就不能介入它了.
有什么想法吗?
所以我试图匹配一个正则表达式,我在这方面相当新.我使用了验证器,当我粘贴代码时它可以工作,但是当它放在.NET2.0 C#页面的代码隐藏中时却没有.
违规代码应该能够在单个分号上分割,但不能在双分号上分割.但是,当我使用字符串时
"条目; ENTRY2; entry3; entry4;"
我得到一个包含空值,上一个条目的最后一个字母和分号本身的无意义数组.在线javascript验证器正确分割它.请帮忙!
我的正则表达式:
((;;|[^;])+)
Run Code Online (Sandbox Code Playgroud) 我需要检查三个条件:
if @filter = 1
{
select * from employeestable where rating is not null
}
else if @filter = 2
{
select * from employeestable where rating is null
}
else
{
select * from employeestable
}
Run Code Online (Sandbox Code Playgroud)
我需要使用case语句.现在我有超过30行查询,如果我使用案例我可以减少我的代码高达70%
请现在让我知道我怎么能这样做.
我如何previous使用OleDbDataReader.
我知道 DataReader 只是使用 DataReader.Read() 前进到下一行,但是我怎样才能回到上一行。???!
我有一个复杂的WCF Rest服务,它接受多个输入和对象.我不能简单地通过在Fiddler中进行HTTP POST来调用它,因为有太多的数据要提供(我可以,但它将永远带我).所以我想在代码中使用代理来完成它.有没有办法为.NET 4 WCF Rest服务生成代理?否则,您建议我允许我轻松测试服务?
谢谢.
TreeNode上IEnumerator的有效参数是什么?
我在这一行得到了错误:
IEnumerator ie = tn.Nodes.GetEnumerator();
Run Code Online (Sandbox Code Playgroud)
在这个方法中:
private void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
_nodeToString += ctn.Text;
}
else
{
_nodeToString += "<" + ctn.Text + ">";
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
_nodeToString += "</" + parentnode + ">";
_nodeToString += "\n";
}
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×4
unit-testing ×2
asp.net ×1
asp.net-mvc ×1
datareader ×1
dllimport ×1
effort ×1
mocking ×1
nunit ×1
regex ×1
rest ×1
rhino-mocks ×1
specflow ×1
tree-nodes ×1
vb.net ×1
wcf ×1
wcf-rest ×1