tdd

Unit testing a class that uses a Timer

二次信任 提交于 2019-11-29 02:50:30
I've got a class that has a private member that has for type System.Windows.Forms.Timer . There's also a private method that is being called every time my timer ticks. Is it worth testing the method? (since it's private) How can I test it? (I know I can have my test class inheriting the class I want to test...) Should I be mocking my timer? Because if I have to test a class that uses an internal timer, my tests may take a lot of time to complete, right? edit: Actually, the method has a dependency on timing, here's the code: private void alertTick(object sender, EventArgs e) { if

Moq - How to verify that a property value is set via the setter

孤者浪人 提交于 2019-11-29 02:48:37
Consider this class: public class Content { public virtual bool IsCheckedOut {get; private set;} public virtual void CheckOut() { IsCheckedOut = true; } public virtual void CheckIn() { //Do Nothing for now as demonstrating false positive test. } } The Checkin method is intentionally empty. Now i have a few test methods to verify the status of calling each method. [TestMethod] public void CheckOutSetsCheckedOutStatusToTrue() { Content c = new Content(); c.CheckOut(); Assert.AreEqual(true, c.IsCheckedOut); //Test works as expected } [TestMethod] public void CheckInSetsCheckedOutStatusToFalse() {

Why does phpunit not show any errors in the console

那年仲夏 提交于 2019-11-29 02:07:05
问题 I'm using phpunit with Laravel 4 framework. Why is it that when there's a PHP error during the tests, no error messages are shown (eg: missing method)? How can we get phpunit to show all errors? 回答1: I think the problem is probably refers to a PHP itself, not PHPUnit. Follow this steps: 1. Check proper php.ini . Note that some systems can use different php.ini for different PHP SAPI: php -i | grep php.ini Configuration File (php.ini) Path => /etc/php5/cli Loaded Configuration File => /etc

快速JavaEE轻量级框架&公用业务模块 设计&实现 6.1

落爺英雄遲暮 提交于 2019-11-29 01:22:39
使用unitils的dbunit模块进行测试。 真正的去访问数据库,每次测试之前打开一个事务,插入测试数据,业务操作,断言测试数据,回滚。 其中unitils+dbunit实现了除业务操作之外的所有步骤。 dbunit提供了将xml直接转换为数据库数据的功能。 unitils则进一步封装,提供了@DataSet,@ExpectDataset以及事务管理等功能,可以通过annotation的方式将数据文件导入数据库,也可以通过xml去断言数据,并且支持自动回滚,超级方便。 这里在使用 unitils整合dbunit的时候碰到了两个问题,记录一下。 关于问题,请分别查看下面两篇文章: unitils使用@DataSet插入测试数据,测试结束后不能回滚 mysql+unitils用@DataSet,抛NoSuchColumnException 最后,贴上一段标准DAO测试的代码: @DataSet public final class RoleDaoTest extends IntegrateBaseTest { @SpringBeanByType private RoleDao roleDao; @Test @ExpectedDataSet public void save() { Role role = new Role(); role.setName("Test Role");

How do I unit test a custom ActionFilter in ASP.Net MVC

狂风中的少年 提交于 2019-11-29 01:11:27
So I'm creating a custom ActionFilter that's based mostly on this project http://www.codeproject.com/KB/aspnet/aspnet_mvc_restapi.aspx . I want a custom action filter that uses the http accept headers to return either JSON or Xml. A typical controller action will look like this: [AcceptVerbs(HttpVerbs.Get)] [AcceptTypesAttribute(HttpContentTypes.Json, HttpContentTypes.Xml)] public ActionResult Index() { var articles = Service.GetRecentArticles(); return View(articles); } The custom filter overrides the OnActionExecuted and will serialize the object (in this example articles) as either JSON or

How to check if method has an attribute

梦想的初衷 提交于 2019-11-29 01:09:29
I have an example class public class MyClass{ ActionResult Method1(){ .... } [Authorize] ActionResult Method2(){ .... } [Authorize] ActionResult Method3(int value){ .... } } Now what I want is to write a function returning true/false that can be executed like this var controller = new MyClass(); Assert.IsFalse(MethodHasAuthorizeAttribute(controller.Method1)); Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method2)); Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method3)); I got to the point where public bool MethodHasAuthorizeAttribute(Func<int, ActionResult> function) { return

Unit testing private code [duplicate]

孤街醉人 提交于 2019-11-29 01:05:58
问题 This question already has answers here : Unit testing private methods in C# (10 answers) Closed 2 years ago . I am currently involved in developing with C# - Here is some background: We implement MVP with our client application and we have a cyclomatic rule which states that no method should have a cyclomatic complexity greater than 5. This leads to a lot of small private methods which are generally responsible for one thing. My question is about unit testing a class: Testing the private

What should a “unit” be when unit testing?

坚强是说给别人听的谎言 提交于 2019-11-29 00:39:49
问题 On Proggit today I was reading the comment thread on a submission entitled, "Why Unit Testing Is A Waste of Time". I'm not really concerned with premise of the article so much as I am with a comment made concerning it: The stem of the problem is that most “units” of code in business software projects are trivial. Change the size of the unit until it is no longer trivial? Who the hell defined the unit of code as a single function or method anyway!? and Well, some of the guys I worked with

TDD …how?

瘦欲@ 提交于 2019-11-28 23:49:51
I'm about to start out my first TDD (test-driven development) program, and I (naturally) have a TDD mental block..so I was wondering if someone could help guide me on where I should start a bit. I'm creating a function that will read binary data from socket and parses its data into a class object. As far as I see, there are 3 parts: 1) Logic to parse data 2) socket class 3) class object What are the steps that I should take so that I could incrementally TDD? I definitely plan to first write the test before even implementing the function. The issue in TDD is "design for testability" First, you

Is there such a thing as excessive unit testing? [closed]

一个人想着一个人 提交于 2019-11-28 23:19:02
I'm not brand new to the concept of unit testing but at the same time I've not yet mastered them either. The one question that has been going through my head recently as I've been writing unit tests while writing my code using the TDD methodology is: to what level should I be testing? Sometimes I wonder if I'm being excessive in the use of unit testing. At what point should a developer stop writing unit tests and get actual work done? I might need to clarify that question before people assume I'm against using TDD... What I'm struggling with is the granularity of my test.... When my app has a