tdd

Starting a TDD project from scratch

删除回忆录丶 提交于 2019-12-04 10:04:23
问题 I read a lot of question and answer on TDD and unit testing on SO but I found nothing that answer to that: where do I start from? Me and team already done a couple of project in which we adopted the use of unit testing, for our code... but code first and then unit testing. At some point of the development process, it became quite natural to write the test first and then the code, bringing us to a more TDD style. Now we would like to make the next step and try to start a new project with TDD

How can I mock Server.HtmlEncode

孤街醉人 提交于 2019-12-04 10:00:57
I am trying the following, but I am getting : Object reference not set to an instance of an object. HttpContextBase mockContext = MockRepository.GenerateMock<HttpContextBase>(); mockContext.Expect(c => c.Server.HtmlEncode("")).IgnoreArguments().Return(""); mockContext.Expect(c => c.Server.HtmlDecode("")).Return(""); controller.ControllerContext = new ControllerContext(mockContext, new RouteData(), controller); Matin, Thanks. That was enough to point me in the right direction provided here: var mockContext = MockRepository.GenerateMock<HttpContextBase>(); var mockServer = MockRepository

C#: How would you unit test GetHashCode?

馋奶兔 提交于 2019-12-04 09:55:08
问题 Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method? 回答1: Test that two distinct objects which are equal have the same hash code (for various values). Check that non-equal objects give different hash codes, varying one aspect/property at a time. While the hash codes don't have to be different, you'd be really unlucky to pick different values for properties which happen to give the same hash code unless you've got a

Stubbing Chained Methods with Rspec

♀尐吖头ヾ 提交于 2019-12-04 09:20:42
问题 I want to call a named_scope that will only return one record, but the named_scope returns an array, that's not a big deal as I can just chain it with .first: Model.named_scope(param).first and this works, what I am struggling with is how to stub the chained call. Does anyone have a reference or an answer on how I would go about achieving this with Rspec mocking? 回答1: I figured something out. Client.stub!(:named_scope).and_return(@clients = mock([Client])) @clients.stub!(:first).and_return(

How can I mock with a block in minitest?

狂风中的少年 提交于 2019-12-04 09:15:44
Hopefully a simple question for MiniTest folks.. I have a section of code which I'll condense into an example here: class Foo def initialize(name) @sqs = Aws::SQS::Client.new @id = @sqs.create_queue( queue_name: name ).fetch(:queue_url) @poller = Aws::SQS::QueuePoller.new(@id) end def pick_first @poller.poll(idle_timeout: 60) do |message| process_msg(message) if some_condition(message) end end How can I mock/stub/something-else so that I can feed a message through to be tested by the some_condition() and possibly processed with process_msg() ? I.e. I want to test the @poller.poll(idle_timeout:

How do you unit-test a TCP Server? Is it even worth it?

好久不见. 提交于 2019-12-04 09:03:02
问题 I'm developing a small TCP server, which will process some TCP packets and behave differently based on the request. How can I write unit test for this? If it's really hard to write, is it even worth the effort? 回答1: The first thing to do is to separate the behavior(s) from the TCP packets that are incoming. Abstract that into a dispatch table or other such thing. Then write unit tests for each of the behaviors independent of how they were invoked. You can test the final TCP -> behavior layer

Is it OK to copy & paste unit-tests when the logic is basically the same?

喜夏-厌秋 提交于 2019-12-04 08:57:46
问题 I currently have like 10 tests that test whenever my Tetris piece doesn't move left if there is a piece in the path, or a wall. Now, I will have to test the same behaviour for the right movement. Is it too bad if I just copy the 10 tests I already have for the left movement and make only the needed changes and do the same for the code itself too? Or should I go again and make each test from the beginning, even so if the logic is basically the same? 回答1: Try taking the 3rd approach that you

Red, green, refactor - why refactor?

北城以北 提交于 2019-12-04 08:28:30
问题 I am trying to learn TDD and unit testing concepts and I have seen the mantra: "red, green, refactor." I am curious about why should you refactor your code after the tests pass? This makes no sense to me, because if the tests pass, then why are you messing with the code? I also see TDD mantras like "only write enough code to make the test pass." The only reason I could come up with, is if to make the test pass with green, you just sloppily write any old code. You just hack together a solution

App.config in Test Projects

China☆狼群 提交于 2019-12-04 07:57:09
问题 I'm building an ASP.NET app in VS2010. I have a number of separate assemblies (class libraries) and corresponding Test projects for each. In one of the class libraries I use an App.config file to store settings. The assembly itself uses the following code to retrieve settings: string tmp = ConfigurationManager.AppSettings["mySetting"]; The problem is that when I try to create a Unit Test in a separate test project, the test does not pick up the setting in the App.config file. If I COPY the

TDD, DDD and Encapsulation

房东的猫 提交于 2019-12-04 07:53:20
问题 After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a great fit for the complexity of the software we write. However, many of the TDD samples I have seen call a method on the domain object and then test properties of the object to ensure the behaviour executed correctly. On the other hand, several