Wednesday, January 25, 2017

FakeItEasy essentials

Today I had a closer look on FakeItEasy. A mockup (or faking) framework to create objects which can be configured freely from outside to be able to unit test classes working with these objects. Additionally it can create dummies (unneeded objects created to satisfy an interface).

FakeItEasy can be installed using nuget without any dependencies. Internally it uses the castle project (which makes me believe that FakeItEasy is more or less a super-powerfull dynamic proxy).


  • A.Fake<class or interface>(); (also CollectionOfFake)
    • tons of creation options can be added in overloaded Fake functions
      • e.g: WithArgumentsForConstructor, Implements,...
    • most interesting (for me) is .CallsBaseMethods so any object can be wrapped and be used with fakeiteasy magic
  • A.CallTo(...); // Properties: A.CallToSet
    • Arguments:
      • exact: "1", "xyz",..
      • by type: A<string>._
    • Conditions: WithReturnType, Where, To
    • ReturnValues: Throws, Returns, ReturnsNextFromSequence, ReturnsLayzily, ThrowsAsync, AssignOutAndRefParameters, AssignOutAndRefParametersLazily
    • Behaviors: DoesNothing, CallsBaseMethod, Invoke
    • Matchers: MustHaveHappened (Repeated), That.Matches(...)
  • Raise.Wtih


Restrictions:

  • can not be used with static or sealed classes 
  • methods that are not virtual or abstract can not be overriden

It took me about 3 hours to read the docs and to test my sample, but I haven't found any show stoppers... Going to use it in my tests and look forward to write more about it...

Saturday, January 21, 2017

Check for usb-sticks in windows

Hi,

I wanted to check in my app whether usb sticks are connected or not (automatic recognition)...

Some research later I found the following article:

https://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives

works quite straight forward ...

things to mention:
- for different use-cases it might be enough to check Win32_LogicalDisk where drivetype=2

- in wpf you might use something like:

1
2
3
4
5
6
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

- when you disconnect a stick and call the wmi to show you data Win32_DiskDrive / Win32_LogicalDisk you get exception ... sleep and retry works after about 5 seconds.

- you can check the result in command line using "wmic logicaldisk get"

kr,
Daniel

Wednesday, January 18, 2017

C# code generation for databases

Hi,

today I found SQLMetal.exe... https://msdn.microsoft.com/en-us/library/bb386987(v=vs.110).aspx

It is part of the visual studio installation and can create c# and vb code for different kind of db artefacts... I am curious how this works in comparison to Entity Framework (= T4).

kr,
Daniel

Tuesday, January 3, 2017

vim as a command line util

For quick find, edit, search and/or replace actions vi | vim | gvim is a perfect tool. A research question for me was: is it also a tool for automation? I wanted to search and replace strings with vi-syntax through a batch file (yeah... using windows makes the thing even more special) which can be scheduled or called on demand.

... and yes... it works. see: http://stackoverflow.com/questions/6945558/calling-search-and-replace-functions-for-vim-from-within-a-bash-script ... the trick is to start in "Ex-Mode" ... (see: documentation), but unfortunately it did not worked for me... I was not able to make it work in my script... no idea why... but:


then I found the -c option which is simple and works perfectly... -c executes a command like substitutions or other stuff and can be used in a chain of "-c"s.



my test-environment:
echo. >> data.txt
del data.txt
echo data data data > data.txt
cls

type data.txt
gvim -c %%s/data/0101/g -c wq data.txt
echo _____________________________________________________
type data.txt

it outputs data before the substitution and 0101 after it... perfect!

kr,
Daniel