tdd

Writing/implementing an API: testability vs information hiding

痞子三分冷 提交于 2019-12-02 03:56:29
问题 Many times I am involved in the design/implementation of APIs I am facing this dilemma. I am a very strong supporter of information hiding and try to use various techniques for that, including but not limited to inner classes, private methods, package-private qualifiers, etc. The problem with these techniques is that they tend to prevent good testability. And while some of these techniques can be resolved (e.g. package-privateness by putting a class into the same package), others are not so

Mocking values in TDD

早过忘川 提交于 2019-12-02 01:34:57
In the book GOOS . It is told not to mock values , which leaves me confused. Does it means that values don't have any behavior? I dont' much knowledge about the value object but AFAIK the value objects are those which are immutable. Is there any heuristic on deciding when to create a value object? Not all immutable objects are value objects. By the way, when designing, consider that the ideal object has only immutable fields and no-arg methods. Regarding the heuristic, a valid approach can be considering how objects will be used: if you build an instance, invoke some methods and then are done

How does Mocha know that done was specified?

浪子不回头ぞ 提交于 2019-12-02 01:03:09
问题 If I write an asynchronous test using Mocha, all I need to do is to specify the done parameter on the test function: test('foo', function (done) {...}); My question is: How does Mocha know whether done was given? The definition of the test function should be something such as function test(title, fn) {...}; How does Mocha check fn? 回答1: It uses the .length property on the test function. To illustrate, try this in the Node REPL: > (function() {}).length 0 > (function(done) {}).length 1 Here's

Level of detail of your unit tests [closed]

筅森魡賤 提交于 2019-12-02 00:24:00
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . I wanted to start a discussion about the details that you cover in your unit tests. Do you test major functionalities, that consist of several methods doing one task at once with one test? or maybe you even test automatic properties? Because, for example, I see little value

How to test 2 methods with common logic?

廉价感情. 提交于 2019-12-01 23:34:04
Let's say that I have 2 public methods: func didSelect(data: Data) { // do something self.view.showText(textForData(data)) } func didDismiss(data: Data) { if data.isSomething { self.view.showText(textForData(data)) } ... } private func textForData(data: Data): String { var text: String if data.distance == nil { text = "..." } else if data.distance < 1000 { text = "\(data.distance) m" } else { text = "\(data.distance / 1000) km" } return text } Both of them depend on the formatting logic of textForData . textForData has (with this minimized implementation) 3 possible cases. If I do test every

Level of detail of your unit tests [closed]

孤人 提交于 2019-12-01 21:27:22
I wanted to start a discussion about the details that you cover in your unit tests. Do you test major functionalities, that consist of several methods doing one task at once with one test? or maybe you even test automatic properties? Because, for example, I see little value in writing a test that would test only this: public Email { set { if(Regex.Match(/*....*/)) email = value; } get { return email; } } As it's really clear and it's just a waste of time. Usually when I do unit tests I test a whole task - like on this example - a whole registration procedure. I'm asking this because, currently

How to assert that an action was called

六月ゝ 毕业季﹏ 提交于 2019-12-01 20:50:12
I need to asset an action called by a mock component. public interface IDispatcher { void Invoke(Action action); } public interface IDialogService { void Prompt(string message); } public class MyClass { private readonly IDispatcher dispatcher; private readonly IDialogservice dialogService; public MyClass(IDispatcher dispatcher, IDialogService dialogService) { this.dispatcher = dispatcher; this.dialogService = dialogService; } public void PromptOnUiThread(string message) { dispatcher.Invoke(()=>dialogService.Prompt(message)); } } ..and in my test.. [TestFixture] public class Test { private

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

做~自己de王妃 提交于 2019-12-01 20:33:21
问题 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

How does Mocha know that done was specified?

穿精又带淫゛_ 提交于 2019-12-01 20:27:34
If I write an asynchronous test using Mocha, all I need to do is to specify the done parameter on the test function: test('foo', function (done) {...}); My question is: How does Mocha know whether done was given? The definition of the test function should be something such as function test(title, fn) {...}; How does Mocha check fn? It uses the .length property on the test function. To illustrate, try this in the Node REPL: > (function() {}).length 0 > (function(done) {}).length 1 Here's the actual line in the source where this check happens: this.async = fn && fn.length; 来源: https:/

Should we unit test console outputs?

偶尔善良 提交于 2019-12-01 20:09:04
问题 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)