tdd

MSTEST - async Testinitialize guarantee test fail

江枫思渺然 提交于 2019-11-28 02:47:54
问题 Just wondering, if anyone thought like this: This is incorrect design to have async call within TestInitialize, as TestInitialize has to happen before any TestMethod. Can this be correct approach in any way to have async TestInitialize as well? private int val = 0; [TestInitialize] public async Task TestMehod1() { var result = await LongRunningMethod(); val = 10; } [TestMethod] public void TestMehod2() { Assert.AreEqual(10, val); } any thought? 回答1: Probably the cleanest way to do this is to

What do I use instead of Whitebox in Mockito 2.2 to set fields?

一个人想着一个人 提交于 2019-11-28 02:34:57
问题 When using Mockito 1.9.x I have been using Whitebox to set values of fields to "inject" mocks. Se example below: @Before public void setUp() { eventHandler = new ProcessEventHandler(); securityService = new SecurityServiceMock(); registrationService = mock(RegistrationService.class); Whitebox.setInternalState(eventHandler, "registrationService", registrationService); Whitebox.setInternalState(eventHandler, "securityService", securityService); } I really like this approach, but now that I

Best Practices of Test Driven Development Using C# and RhinoMocks [closed]

最后都变了- 提交于 2019-11-28 02:32:25
In order to help my team write testable code, I came up with this simple list of best practices for making our C# code base more testable. (Some of the points refer to limitations of Rhino Mocks, a mocking framework for C#, but the rules may apply more generally as well.) Does anyone have any best practices that they follow? To maximize the testability of code, follow these rules: Write the test first, then the code. Reason: This ensures that you write testable code and that every line of code gets tests written for it. Design classes using dependency injection. Reason: You cannot mock or test

MSTest Code Coverage

谁说胖子不能爱 提交于 2019-11-28 02:29:19
问题 Is there a way to test code coverage within visual studio if I'm using MSTest? Or do I have to buy NCover? Is the NCover Enterprise worth the money or are the old betas good enough if Microsoft doesn't provide built in tools to do code coverage? EDIT: Description of VS Products and which ones include code coverage https://www.visualstudio.com/vs/compare/ TestDriven.NET (http://testdriven.net/) can be used if your VS version doesn't support it. 回答1: Yes, you can find code coverage information

Type of Projects where Unit Testing is useless [closed]

六月ゝ 毕业季﹏ 提交于 2019-11-28 00:18:48
问题 Do you believe that Unit Testing (and test driven development) must be done under any set of circumstances or should there be some exceptions. I've been working on the type of projects lately where I can't see how Unit Testing would be useful or improve design, quality of code, etc. One type of project is PDF reports generator which takes aggregate data (values already calculated and QAed) and outputs it to a PDF report file. Another type is straight-forward CRUD applications using 3rd party

How to MOQ an Indexed property

谁说我不能喝 提交于 2019-11-27 22:38:45
I am attempting to mock a call to an indexed property. I.e. I would like to moq the following: object result = myDictionaryCollection["SomeKeyValue"]; and also the setter value myDictionaryCollection["SomeKeyValue"] = myNewValue; I am doing this because I need to mock the functionality of a class my app uses. Does anyone know how to do this with MOQ? I've tried variations on the following: Dictionary<string, object> MyContainer = new Dictionary<string, object>(); mock.ExpectGet<object>( p => p[It.IsAny<string>()]).Returns(MyContainer[(string s)]); But that doesn't compile. Is what I am trying

How to unit-test an action, when return type is ActionResult?

风格不统一 提交于 2019-11-27 20:01:25
I have written unit test for following action. [HttpPost] public ActionResult/*ViewResult*/ Create(MyViewModel vm) { if (ModelState.IsValid) { //Do something... return RedirectToAction("Index"); } return View(vm); } Test method can access Model properties, only when return type is ViewResult . In above code, I have used RedirectToAction so return type of this action can not be ViewResult . In such scenario how do you unit-test an action? So here is my little example: public ActionResult Index(int id) { if (1 != id) { return RedirectToAction("asd"); } return View(); } And the tests: [TestMethod

How do I run unittest on a Tkinter app?

瘦欲@ 提交于 2019-11-27 19:47:54
I've just begun learning about TDD , and I'm developing a program using a Tkinter GUI. The only problem is that once the .mainloop() method is called, the test suite hangs until the window is closed. Here is an example of my code: # server.py import Tkinter as tk class Server(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.mainloop() # test.py import unittest import server class ServerTestCase(unittest.TestCase): def testClassSetup(self): server.Server() # and of course I can't call any server.whatever functions here if __name__ == '__main__': unittest.main() What is the appropriate way

Using Dependency Injection with Roboguice?

微笑、不失礼 提交于 2019-11-27 19:09:35
问题 I'm working on an Android project and I would like to know any recommendations about what's a good architecture to build an android application. I want to use dependency injection using Roboguice and I've been reading about MVVM pattern or MVC pattern (Android MVVM Design Pattern Examples). Also I know that roboguice have a pretty cool Context-Based Event's raising and handling feature that could be very testable as the code is decoupled. Any recommendations on a working design pattern? a

Bash and Test-Driven Development

余生长醉 提交于 2019-11-27 17:59:05
When writing more than a trivial script in bash, I often wonder how to make the code testable. It is typically hard to write tests for bash code, due to the fact that it is low on functions that take a value and return a value, and high on functions that check and set some aspect in the environment, modify the file-system, invoke a program, etc. - functions that depend on the environment or have side effects. Thus the setup and test code become much more complicated than the code they test. For example, consider a simple function to test: function add_to_file() { local f=$1 cat >> $f sort -u