tdd

TDD: why might it be wrong to let app code know it is being tested, not run?

有些话、适合烂在心里 提交于 2019-12-01 19:34:10
In this thread , Brian (the only answerer) says "Your code should be written in such a fashion that it is testing-agnostic" The single comment says "Your code should definitely not branch on a global "am I being tested flag".". But neither gives reasons, and I would really like to hear some rational thoughts on the matter. It would be immensely easy (particularly given the fact that a lot of tests have package-private access to the app classes) to reach into a given app class and set a boolean to say "this is a test, not a run". All sorts of things which I find myself jumping through hoops

Should we unit test console outputs?

匆匆过客 提交于 2019-12-01 19:19:10
I am working with some legacy code that has some System.out.print commands in itself. My eCobertura plugin shows this lines red, so I want to unit test them. Here in stackoverflow I found a way to unit test console outputs which i thing is very interesting. This is how I do it: private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); } @After public void cleanUpStreams() { System.setOut(null); } @Test public void out() { System.out.print("Some message from the system"); assertEquals("Some

Do shoulda-matchers' ActiveRecord matchers violate the “test behavior not implementation” rule?

折月煮酒 提交于 2019-12-01 18:57:28
For example, if I am using should validate_presence_of in my spec, that's only testing that I have the validate_presence_of piece of code inside my model, and that's testing implementation. More importantly, isn't that spec totally useless for testing the real problem, which is "if I don't fill out a certain field, will the model be saved successfully?" Some of shoulda-matchers' matchers don't test implementation, they test behavior. For example, look at the source for allow_value (which validate_presence_of uses): #matches? actually sets the instance's attribute to the value and checks to see

How to fix err Jest has detected the following 3 open handles potentially keeping Jest from exiting

眉间皱痕 提交于 2019-12-01 17:44:17
Just starting to work on some node app using jest for testing. express-generator used for scaffolding. On first test I get following error: Jest has detected the following 3 open handles potentially keeping Jest from exiting Steps to reproduce: git clone git@github.com:gandra/node-jest-err-demo.git cd node-jest-err-demo npm install cp .env.example .env npm run test npx envinfo --preset jest output: npx: installed 1 in 1.896s System: OS: macOS High Sierra 10.13.4 CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz Binaries: Node: 9.3.0 - /usr/local/bin/node Yarn: 1.5.1 - /usr/local/bin/yarn npm:

How to initialize constant string for multiple tests in google test?

你说的曾经没有我的故事 提交于 2019-12-01 17:25:55
问题 I'm using google test and I have a cpp-file containing several tests. I would like to initialize a string with the current date and time when starting the first test. I would like to use this string in all other tests, too. How can I do this. I've tried the following ( m_string being a protected member of CnFirstTest ), but it didn't work (since the constructor and SetUp will be called before each test): CnFirstTest::CnFirstTest(void) { m_string = currentDateTime(); } void CnFirstTest::SetUp(

How can I test void methods? [duplicate]

两盒软妹~` 提交于 2019-12-01 17:23:40
This question already has an answer here: Unit testing void methods? 11 answers I have some void methods and I need to test them, but I'm not sure about how to do it. I just know how to test methods that return something, using Assert. Someone knows how to do it? Do you guys know some links with exercices in this style? You can test two things: State changes after void method call (state-based testing) Interaction with dependencies during void method call (interaction testing) First approach is simple (NUnit sample): var sut = new Sut(); sut.Excercise(foo); Assert.That(sut.State, Is.EqualTo

Should I unit-test with data that should not be passed in a function (invalid input)?

会有一股神秘感。 提交于 2019-12-01 16:54:36
I am trying to use TDD for my coding practice. I would like to ask should I test with a data that should not happen in a function BUT this data may possibly break your program. Here is one of a easy example to illustrate to what I ask : a ROBOT function that has a one INT parameter. In this function I know that the valid range would only be 0-100. If -1, 101 is used, the function will be break. function ROBOT (int num){ ... ... ... return result; } So I decided some automated test cases for this function... 1. function ROBOT with input argument 0 2. function ROBOT with input argument 1 3.

Mocking Guid.NewGuid()

筅森魡賤 提交于 2019-12-01 16:52:40
问题 Suppose I have the following entity: public class User { public int Id { get; set; } public string Username { get; set; } public Guid UserGuid { get; set; } public Guid ConfirmationGuid { get; set; } } And the following interface method: void CreateUser(string username); Part of the implementation should create two new GUIDs: one for UserGuid , and another for ConfirmationGuid . They should do this by setting the values to Guid.NewGuid() . I already have abstracted Guid.NewGuid() using an

Should an Aggregate Root Implement an Interface in Domain-Driven Design

﹥>﹥吖頭↗ 提交于 2019-12-01 16:46:53
I'm working on a project using both domain-driven design and test-driven development. While reading through the DDD book by Evans, I noticed that he did not define interfaces for aggregate roots in the domain. If I'm doing both DDD and TDD, should I define interfaces for each aggregate root to make the aggregate root classes easily testable and mockable? If so, should I also define interfaces for each entity contained within the aggregate root as well? From my searches on Google and StackOverflow, I've found answers that come close to what I'm looking for, but I'm specifically look for advice

Mocking Guid.NewGuid()

旧城冷巷雨未停 提交于 2019-12-01 16:46:29
Suppose I have the following entity: public class User { public int Id { get; set; } public string Username { get; set; } public Guid UserGuid { get; set; } public Guid ConfirmationGuid { get; set; } } And the following interface method: void CreateUser(string username); Part of the implementation should create two new GUIDs: one for UserGuid , and another for ConfirmationGuid . They should do this by setting the values to Guid.NewGuid() . I already have abstracted Guid.NewGuid() using an interface: public interface IGuidService { Guid NewGuid(); } So I can easily mock this when only one new