tdd

Rspec: expect vs expect with block - what's the difference?

旧时模样 提交于 2019-11-26 13:49:19
问题 Just learning rspec syntax and I noticed that this code works: context "given a bad list of players" do let(:bad_players) { {} } it "fails to create given a bad player list" do expect{ Team.new("Random", bad_players) }.to raise_error end end But this code doesn't: context "given a bad list of players" do let(:bad_players) { {} } it "fails to create given a bad player list" do expect( Team.new("Random", bad_players) ).to raise_error end end It gives me this error: Team given a bad list of

How do I mock static methods in a class with easymock?

吃可爱长大的小学妹 提交于 2019-11-26 12:27:41
问题 Suppose I have a class like so: public class StaticDude{ public static Object getGroove() { // ... some complex logic which returns an object }; } How do I mock the static method call using easy mock? StaticDude.getGroove() . I am using easy mock 3.0 回答1: Not sure how to with pure EasyMock, but consider using the PowerMock extensions to EasyMock. It has a lot of cool functions for doing just what you need - https://github.com/jayway/powermock/wiki/MockStatic 回答2: Easymock is a testing

How do you mock the session object collection using Moq

一世执手 提交于 2019-11-26 12:21:37
问题 I am using shanselmann\'s MvcMockHelper class to mock up some HttpContext stuff using Moq but the issue I am having is being able to assign something to my mocked session object in my MVC controller and then being able to read that same value in my unit test for verification purposes. My question is how do you assign a storage collection to the mocked session object to allow code such as session[\"UserName\"] = \"foo\" to retain the \"foo\" value and have it be available in the unit test. 回答1

What do programmers mean when they say, “Code against an interface, not an object.”?

笑着哭i 提交于 2019-11-26 12:16:50
问题 I\'ve started the very long and arduous quest to learn and apply TDD to my workflow. I\'m under the impression that TDD fits in very well with IoC principles. After browsing some of TDD tagged questions here in SO, I read it\'s a good idea to program against interfaces, not objects. Can you provide simple code examples of what this is, and how to apply it in real use cases? Simple examples is key for me (and other people wanting to learn) to grasp the concepts. 回答1: Consider: class MyClass {

Unit Testing bash scripts

这一生的挚爱 提交于 2019-11-26 11:52:05
问题 We have a system that has some bash scripts running besides Java code. Since we are trying to Test Everything That Could Possibly Break, and those bash scripts may break, we want to test them. The problem is it is hard to test bash scripts. Is there a way or a best practice to test bash scripts? Or should we quit using bash scripts and look for alternative solutions that are testable? 回答1: There is actually a shunit2, an xUnit based unit test framework for Bourne based shell scripts. I haven

Mockito 简明教程

三世轮回 提交于 2019-11-26 11:33:37
原文同步至 http://waylau.com/mockito-quick-start/ Mock 测试是单元测试的重要方法之一。本文介绍了基于 Java 语言的 Mock 测试框架 -- Mockito 的使用。 什么是 Mock 测试 Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造出来)或者不容易获取比较复杂的对象(如 JDBC 中的ResultSet 对象),用一个虚拟的对象(Mock 对象)来创建以便测试的测试方法。 Mock 最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。 比如一段代码有这样的依赖: 当我们需要测试A类的时候,如果没有 Mock,则我们需要把整个依赖树都构建出来,而使用 Mock 的话就可以将结构分解开,像下面这样: Mock 对象使用范畴 真实对象具有不可确定的行为,产生不可预测的效果,(如:股票行情,天气预报) 真实对象很难被创建的 真实对象的某些行为很难被触发 真实对象实际上还不存在的(和其他开发小组或者和新的硬件打交道)等等 使用 Mock 对象测试的关键步骤 使用一个接口来描述这个对象 在产品代码中实现这个接口 在测试代码中实现这个接口 在被测试代码中只是通过接口来引用对象

Mocking Static methods using Rhino.Mocks

萝らか妹 提交于 2019-11-26 09:44:04
问题 Is it possible to mock a static method using Rhino.Mocks? If Rhino does not support this, is there a pattern or something which would let me accomplish the same? 回答1: Is it possible to mock a static method using Rhino.Mocks No, it is not possible. TypeMock can do this because it utilizes the CLR profiler to intercept and redirect calls. RhinoMocks, NMock, and Moq cannot do this because these libraries are simpler; they don't use the CLR profiler APIs. They are simpler in that they use proxies

Random data in Unit Tests?

瘦欲@ 提交于 2019-11-26 08:58:12
问题 I have a coworker who writes unit tests for objects which fill their fields with random data. His reason is that it gives a wider range of testing, since it will test a lot of different values, whereas a normal test only uses a single static value. I\'ve given him a number of different reasons against this, the main ones being: random values means the test isn\'t truly repeatable (which also means that if the test can randomly fail, it can do so on the build server and break the build) if it\

Why should I practice Test Driven Development and how should I start?

懵懂的女人 提交于 2019-11-26 08:49:40
问题 Lots of people talk about writing tests for their code before they start writing their code. This practice is generally known as Test Driven Development or TDD for short. What benefits do I gain from writing software this way? How do I get started with this practice? 回答1: There are a lot of benefits: You get immediate feedback on if your code is working, so you can find bugs faster By seeing the test go from red to green, you know that you have both a working regression test, and working code

How do I test database-related code with NUnit?

冷暖自知 提交于 2019-11-26 08:44:37
问题 I want to write unit tests with NUnit that hit the database. I\'d like to have the database in a consistent state for each test. I thought transactions would allow me to \"undo\" each test so I searched around and found several articles from 2004-05 on the topic: http://weblogs.asp.net/rosherove/archive/2004/07/12/180189.aspx http://weblogs.asp.net/rosherove/archive/2004/10/05/238201.aspx http://davidhayden.com/blog/dave/archive/2004/07/12/365.aspx http://haacked.com/archive/2005/12/28/11377