tdd

Database integration tests

自古美人都是妖i 提交于 2019-12-04 03:14:25
When you are doing integration tests with either just your data access layer or the majority of the application stack. What is the best way prevent multiple tests from clashing with each other if they are run on the same database? Transactions. What the ruby on rails unit test framework does is this: Load all fixture data. For each test: BEGIN TRANSACTION # Yield control to user code ROLLBACK TRANSACTION End for each This means that Any changes your test makes to the database won't affect other threads while it's in-progress The next test's data isn't polluted by prior tests This is about a

URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

南楼画角 提交于 2019-12-04 02:53:07
问题 I get URI::InvalidURIError testing Rails Home controller: require 'test_helper' class HomeControllerTest < ActionDispatch::IntegrationTest test "should get index" do get :index assert_response :success end end get the following error: E Error: HomeControllerTest#test_should_get_index: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index test/controllers/home_controller_test.rb:7:in `block in <class:HomeControllerTest>' The stack is the following: Rails 5.0.0.beta3

Cobra + Viper Golang How to test subcommands?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:08:44
I am developing an web app with Go. So far so good, but now I am integrating Wercker as a CI tool and started caring about testing. But my app relies heavily on Cobra/Viper configuration/flags/environment_variables scheme, and I do not know how to properly init Viper values before running my test suite. Any help would be much appreciated. When I use Cobra/Viper or any other combination of CLI helpers, my way of doing this is to have the CLI tool run a function whose sole purpose will be to get arguments and pass them to another method who will do the actual work. Here is a short (and dumb)

Is Test Driven Development good for a starter? [closed]

不想你离开。 提交于 2019-12-04 02:03:20
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Expanding this question on how I learnt to pass from problem description to code Two people mentioned TDD. Would it be good for a starter to get into TDD ( and avoid bad habits in the future ? ) Or would it be too complex for a stage

Can I mock a super class method call?

旧巷老猫 提交于 2019-12-04 01:18:42
Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymock or jmock (and I think it is not possible). There is a (relative) clean solution, to create a delegate with the super class method logic and then set expectations on it, but I don't know why and when use that solution ¿any ideas/examples? Thanks Cem Catikkas Well, you can if you want to. I don't know if you are familiar with JMockit , go check it out. The current version is 0.999.17 In the mean time, let's take a look

Should I change the naming convention for my unit tests?

血红的双手。 提交于 2019-12-04 01:18:28
I currently use a simple convention for my unit tests. If I have a class named "EmployeeReader", I create a test class named "EmployeeReader.Tests. I then create all the tests for the class in the test class with names such as: Reading_Valid_Employee_Data_Correctly_Generates_Employee_Object Reading_Missing_Employee_Data_Throws_Invalid_Employee_ID_Exception and so on. I have recently been reading about a different type of naming convention used in BDD. I like the readability of this naming, to end up with a list of tests something like: When_Reading_Valid_Employee (fixture) Employee_Object_Is

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

烈酒焚心 提交于 2019-12-04 00:29:55
I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok to use them sometimes? Phil Sandler Never say never--static classes and methods have their place in your toolbox. That said, if the class you are trying to isolate and test (subject under test or SUT) depends on a static class or method, you will be unable to write a test that isolates the SUT from that static dependency--when your test code runs it

Mocking file objects or iterables in python

南楼画角 提交于 2019-12-03 23:55:33
问题 Which way is proper for mocking and testing code that iters object returned by open(), using mock library? whitelist_data.py : WHITELIST_FILE = "testdata.txt" format_str = lambda s: s.rstrip().lstrip('www.') whitelist = None with open(WHITELIST_FILE) as whitelist_data: whitelist = set(format_str(line) for line in whitelist_data) if not whitelist: raise RuntimeError("Can't read data from %s file" % WHITELIST_FILE) def is_whitelisted(substr): return 1 if format_str(substr) in whitelist else 0

Are PHP global constants a good modern development practice?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 23:23:13
I'm working on a new project with a sizeable PHP codebase. The application uses quite a few PHP constants ( define('FOO', 'bar') ), particularly for things like database connection parameters. These constants are all defined in a single configuration file that is require_once() 'd directly by basically every class in the application. A few years ago this would have made perfect sense, but since then I've gotten the Unit Testing bug and this tight coupling between classes is really bothering me. These constants smell like global variables, and they're referenced directly throughout the

Moq testing LINQ Where queries

旧街凉风 提交于 2019-12-03 23:22:15
I'm using EF 4.1 to build a domain model. I have a Task class with a Validate(string userCode) method and in it I want to ensure the user code maps to a valid user in the database, so: public static bool Validate(string userCode) { IDbSet<User> users = db.Set<User>(); var results = from u in users where u.UserCode.Equals(userCode) select u; return results.FirstOrDefault() != null; } I can use Moq to mock IDbSet no problem. But ran into trouble with the Where call: User user = new User { UserCode = "abc" }; IList<User> list = new List<User> { user }; var users = new Mock<IDbSet<User>>(); users