Skip to content

Instantly share code, notes, and snippets.

View AlexZeitler's full-sized avatar
👷‍♂️
Building stuff

Alexander Zeitler AlexZeitler

👷‍♂️
Building stuff
View GitHub Profile
public class LightSwitchedOff : IBehaviorConfig {
public void EstablishContext(IDependencyAccessor accessor) {
accessor.The<ILight>().WhenToldTo(l => l.LightState).Return(LightState.Off).Repeat.Once();
accessor.The<ILight>().WhenToldTo
(l => l.SwitchLight(LightState.On)).Do(
new Func<object, LightSwitchedHandlerArgs, int>((sender1, args1) =>
{
accessor.The<ILight>()
@AlexZeitler
AlexZeitler / gist:798238
Created January 27, 2011 08:29
BDD Learning Tests mit BehaviorConfigs
Ich habe ein COM Interop Szenario. Um das Verhalten des API kennen zu lernen, habe ich Learning Tests / Specs geschrieben, die aber eine OLE-Verbindung zur laufenden App benötigen.
Es müssen z.B. Dokumente in der App geladen werden. Da häufig gleiche Dokumente benötigt werden, sollen diese in BehaviorConfigs ausgelagert werden. Da die BehaviorConfigs teilweise voneinander abhängen (schlecht, aber derzeit nicht anders lösbar), muß EstablishContext der jeweiligen BehaviorConfig vor dem Instanzieren der nächsten BehaviorConfig erledigt sein.
public class EmptyAssemblyAsActiveDocument : IBehaviorConfig {
public void EstablishContext(IDependencyAccessor accessor) {
string applicationDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string tempFolder = Path.Combine(applicationDataFolder, "Temp");
DirectoryInfo directoryInfo = new DirectoryInfo(tempFolder);
if (!directoryInfo.Exists) {
@AlexZeitler
AlexZeitler / Failing Spec
Created February 21, 2011 14:00
Why does this always fail?
using System;
using SolidWorks.Interop.sldworks;
using Xunit;
namespace SolidTp.SolidWorks.Domain.Tests {
[Concern(typeof (IEquationManagerWriter))]
public class Wenn_eine_variablendefinition_im_modell_aktualisiert_werden_soll_ : InstanceContextSpecification<IEquationManagerWriter> {
private string _variableDefinitionAsEquation;
protected override void EstablishContext() {
@AlexZeitler
AlexZeitler / gist:917497
Created April 13, 2011 13:04
Configuring the The<>() directly
Running the tests results in "System.InvalidOperationException:
The result for IContactRepository.Get(1); has already been setup."
When creating An<IContactRepository> and assigning it by fakeAccessor.Use(), it does work.
public class When_requesting_alex_from_contact_service : WithSubject<ContactService> {
static Contact _contact;
static IContact _actualContact;
Establish context = () => {
@AlexZeitler
AlexZeitler / gist:923466
Created April 16, 2011 20:30
mfakes, StructureMap and COM...
Establish context = () => {
With<SolidWorks2010OleConnected>();
_solidWorks = The<ISolidWorks>();
}
public class SolidWorks2010OleConnected {
OnEstablish context =
fakeAccessor =>
{
ISldWorks solidWorks =
@AlexZeitler
AlexZeitler / gist:925015
Created April 18, 2011 08:46
SolidWorks and StructureMap RhinoAutoMocker
public class When_putting_a_real_solidworks_instance_into_a_mock {
static ISldWorks _solidWorks;
static IModelDoc2 _model;
static Exception _exception;
static RhinoAutoMocker<ModelReader> _mocks;
Establish context
= () =>
{
@AlexZeitler
AlexZeitler / gist:926928
Created April 19, 2011 06:51
Passing params into BehaviorConfigs by With<T> in Machine.Fakes
How about having
Establish context = () => With<CurrentTime>().UsingParams(new DateTime(2011,2,14));
instead of
Establish context = () => With(new CurrentTime(new DateTime(2011, 2, 14)));
I think it would enable a more consistent manner of reading when using more than one behavior config.
Might be nitpicking, though ;-)
@AlexZeitler
AlexZeitler / gist:927605
Created April 19, 2011 13:16
Can't create mocks of sealed classes
public class ListOfAValidAndAInvalidRequirement {
OnEstablish context =
fakeAccessor =>
{
IRequirement validRequirement = fakeAccessor.An<IRequirement>();
validRequirement.WhenToldTo(r => r.Validate()).Return(true); // throws ex here
IRequirement invalidRequirement = fakeAccessor.An<IRequirement>();
invalidRequirement.WhenToldTo(r=>r.Validate()).Return(false);
IList<IRequirement> requirements = new List<IRequirement>
{
@AlexZeitler
AlexZeitler / gist:934118
Created April 21, 2011 10:28
passing in objects or their members values?
I have to implement a method that does some string formatting based on string property values of two COM objects.
Now there are at least two ways to deal with it...
a) passing in the objects:
public string CreateIdForPartInView(IPartDoc part, IView drawingView) {
IModelDoc2 model = part as IModelDoc2;
string modelPath = model.GetPathName();
string modelName = Path.GetFileNameWithoutExtension(modelPath);
string id = string.Format("{0}-1@{1}", modelName, drawingView.Name);
@AlexZeitler
AlexZeitler / gist:945176
Created April 27, 2011 20:51
rudimentary draft of comparing items of two lists on specified property as machine.specfication extension method
public class Given_two_lists_containing_a_point_each_when_comparing_points_on_x_value {
static IList<Point> _expectedPoints;
static IList<Point> _actualPoints;
Establish context
= () => {
_expectedPoints = new List<Point>
{
new Point {X = 153.6m, Y = 21m, Z = 0}
};