<p>That is very different, and is based on information theory (at a level that anyone can understand). It says that programming can be looked at as a process of defining languages, and skill in doing so is essential for good programming.</p> What's a good way to overwrite DateTime.Now during testing?

It elevates the concept of domain-specific-languages (DSLs). It agrees emphatically with DRY (don't repeat yourself). It gives a big thumbs-up to code generation. It results in software with massively less data structure than is typical for modern applications.

I've got some (C#) code that relies on today's date to correctly calculate things in the future. If I use today's date in the testing, I have to repeat the calculation in the test, which doesn't feel right. What's the best way to set the date to a known value within the test so that I can test that the result is a known value?

45535 次浏览

I think creating a separate clock class for something simple like getting the current date is a bit overkill.

You can pass today's date as a parameter so you can input a different date in the test. This has the added benefit of making your code more flexible.

The real world isn't "OO". The real world is not largely structured from sensible pieces. Instead it's made from chaotically moving particles. The earth is a particle soup. Still people see birds, trees, sky, ground, forests, ponds. OO is about abstraction of program components. It's fundamentally flawed to think about OO for modelling something else than programs.

nents. It's fundamentally flawed to think about OO for modelling something else than programs.

All the money and time failed to make software any better, because it failed to make programmers smarter, also because it failed to change the way how people think about software. "OOP" in the sense you use it is a buzzword used to get the money out from

All the money and time failed to make software any better, because it failed to make programmers smarter, also because it failed to change the way how people think about software. "OOP" in the sense you use it is a buzzword used to get the money out from idiots. Yes, people who have put money on "OOP" education and tools are idiots. People who tend to fall on hoaxes tend to be idiots.

idiots. Yes, people who have put money on "OOP" education and tools are idiots. People who tend to fall on hoaxes tend to be idiots.

The value of "OOP" is the abstraction and the code reuse inside the same program. OOP is meant to be used with imperative programs.

The value of "OOP" is the abstraction and the code reuse inside the same program. OOP is meant to be used with imperative programs.

If you get up from assembly routines. Assembly is an ordered sequences of pairs composed from labels and instructions. Assembly code is similar to the 'particle soup'. Now you can move to the subroutine. Subroutine picks a label from that label:instruction -soup, and hides the rest of labels inside the subroutine. As the effect code becomes more abstract and your namespace stays cleaner.

Now, if you think what subroutines do... Few of decades ago people were thinking that subroutines are at their best when they work on the arguments. That made them to give each object it's own protocol. Protocol would contain label:procedure -pairs. Now called selector:method -pairs. Procedures weren't bound directly to the other procedures anymore, explaining the 'late binding' -term. Along with keeping the history from the protocols (inheritance), this formed the 'object orientation' in the smalltalk.

You've been incapacitated the late binding mechanism and forgotten what inheritance means. And you yet wonder what you are missing there. "Where's the value of OOP, and why has all the time and money failed to make software any better?" - I think you stuffed them into your arse. When you attempt to colonoscopy you will find them.

Think of it as a different sort of tool, not necessarily generally better. A hammer in addition to a screwdriver, say. Perhaps we will eventually get out of the practice of software engineering as knowing which wrench to use to hammer the screw in.

Many people find the 'brain rewiring' process too hard, painful or just too much effort and so spend their life dissin' OOP and so you'll find a lot of OO haters out there but I'm happy about that because it's those people that make people like me look good: "What, you can do it for $X and it will be ready in 2 months and you will give us a maintainable code base!!! Wow, can you start today?"

Real world may not be OO but people tend to learn or think better with analogy (abstraction) rather than logic.

OOP is not for computer but it's for programmer who is poor at figuring out a complex system without analogy. I believe main purpose of OOP is better organization of code using abstraction so without knowledge of other parts, a programmer can easily understand what specific part of or whole system does in a level she/he want to be in.

You could inject the class (better: method/delegate) you use for DateTime.Now in the class being tested. Have DateTime.Now be a default value and only set it in testing to a dummy method that returns a constant value.

EDIT: What Blair Conrad said (he has some code to look at). Except, I tend to prefer delegates for this, as they don't clutter up your class hierarchy with stuff like IClock...

Ayende Rahien uses a static method that is rather simple...

public static class SystemTime
{
public static Func<DateTime> Now = () => DateTime.Now;
}

I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).

d then "M-x clipboard-kill-ring-save"

There are two ways:

    (note you can bind this to an easier
  1. (Applies to unix OS's only) key). Then just "Edit->Paste" in Highlight the desired text with your your favorite app.
mouse (this copies it to the X

Clipboard operations available:

    clipboard) and then middle click to
  • clipboard-kill-ring-save -- copy paste.
  • selection from Emacs to clipboard
  • Highlight the desired text and then "M-x clipboard-kill-ring-save"
  • clipboard-kill-region -- cut (note you can bind this to an easier selection from Emacs to clipboard
  • clipboard-yank -- paste from clipboard to Emacs
key). Then just "Edit->Paste" in

The key to successful unit testing is decoupling. You have to separate your interesting code from its external dependencies, so it can be tested in isolation. (Luckily, Test-Driven Development produces decoupled code.)

your favorite app.

In this case, your external is the current DateTime.

Clipboard operations available:

    My advice here is to extract the logic that deals with the DateTime to a new method or class or whatever makes sense in your case, and pass the DateTime in. Now, your unit test can pass an arbitrary DateTime in, to produce predictable results.

  • clipboard-kill-ring-save -- copy

    I'd suggest using IDisposable pattern:

    [Test]
    public void CreateName_AddsCurrentTimeAtEnd()
    {
    using (Clock.NowIs(new DateTime(2010, 12, 31, 23, 59, 00)))
    {
    string name = new ReportNameService().CreateName(...);
    Assert.AreEqual("name 2010-12-31 23:59:00", name);
    }
    }
    
    selection from Emacs to clipboard
  • In detail described here:

  • clipboard-kill-region -- cut http://www.lesnikowski.com/blog/index.php/testing-datetime-now/

  • static or non-virtual methods. Moles relies on the profiler from Pex.

    Insert the following into your .emacs file:

    (setq x-select-enable-clipboard t)
    

    Using Microsoft Fakes to create a shim is a really easy way to do this. Suppose I had the following class:

    public class MyClass
    {
    public string WhatsTheTime()
    {
    return DateTime.Now.ToString();
    }
    
    
    }
    
    >public class MyClass

    In Visual Studio 2012 you can add a Fakes assembly to your test project by right clicking on the assembly you want to create Fakes/Shims for and selecting "Add Fakes Assembly"

    Adding Fakes Assembly

    {

    Finally, Here is what the test class would look like:

    using System;
    using ConsoleApplication11;
    using Microsoft.QualityTools.Testing.Fakes;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    
    namespace DateTimeTest
    {
    [TestClass]
    public class UnitTest1
    {
    [TestMethod]
    public void TestWhatsTheTime()
    {
    
    
    using(ShimsContext.Create()){
    
    
    //Arrange
    System.Fakes.ShimDateTime.NowGet =
    () =>
    { return new DateTime(2010, 1, 1); };
    
    
    var myClass = new MyClass();
    
    
    //Act
    var timeString = myClass.WhatsTheTime();
    
    
    //Assert
    Assert.AreEqual("1/1/2010 12:00:00 AM",timeString);
    
    
    }
    }
    }
    }
    

    Further reading:

      Let's be careful with our definitions here

    • An Emacs copy is the command kill-ring-save (usually bound to M-w).
    • in an application window).
    • A system copy is what you typically get from pressing C-c (or choosing "Edit->Copy" in a application window).
    • An X paste is pressing the "center mouse button" (simulated by pressing the left and right mouse buttons together).

    In my case (on GNOME):

    • An X copy is "physically" highlighting text with the mouse cursor.
    • Both Emacs and system copy usually work with X paste.
    • An Emacs paste is the command yank (usually bound to C-y).
    • A system paste is what you typically get from pressing C-v (or choosing "Edit-Paste" in an application window).
    • X copy usually works with Emacs paste.
    • To make system copy work with Emacs paste and Emacs copy work with system paste, you need to add (setq x-select-enable-clipboard t) to your .emacs. Or try

      META-X set-variable RET x-select-enable-clipboard RET t
      

    I think this is pretty standard modern Unix behavior.

    It's also important to note (though you say you're using Emacs in a separate window) that when Emacs is running in a console, it is completely divorced from the system and X clipboards: cut and paste in that case is mediated by the terminal. For example, "Edit->Paste" in your terminal window should act exactly as if you typed the text from the clipboard into the Emacs buffer.

    You can install module right from the GUI Nuget Package Manager or by using the command:

    Install-Package -Id DateTimePT -ProjectName Project
    

    I stick this in my .emacs:

    (setq x-select-enable-clipboard t)
    (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
    

    And the code for the Nuget is here.

    I subsequently have basically no problems cutting and pasting back and forth from anything in Emacs to any other X11 or Gnome application.

    Bonus: to get these things to happen in Emacs without having to reload your whole .emacs, do C-x C-e with the cursor just after the close paren of each of those expressions in the .emacs buffer.

    The example of usage with the Autofac can be found here.