Skip to content

Instantly share code, notes, and snippets.

@AlexZeitler
Created April 18, 2011 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexZeitler/925015 to your computer and use it in GitHub Desktop.
Save AlexZeitler/925015 to your computer and use it in GitHub Desktop.
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
= () =>
{
_mocks = new RhinoAutoMocker<ModelReader>();
_mocks.Inject(new SolidWorksOleConnector().Connect(new SolidWorksVersion2010()));
};
Because of
= () => {
_exception = Catch.Exception(()
=> new ModelReader((ISldWorks) _mocks.Container.GetInstance(typeof(ISldWorks))));
};
It should_not_throw_an_exception
= () => { _exception.ShouldBeNull(); };
It should_return_the_active_modeldoc
= () => { _model.ShouldNotBeNull(); };
}
public class ModelReader {
readonly ISldWorks _solidWorks;
public ModelReader(ISldWorks solidWorks) {
_solidWorks = solidWorks;
}
}
@BjRo
Copy link

BjRo commented Apr 18, 2011

No, the T in the RhinoAutoMocker should be a concrete class whose dependencies get filled by the automocker. It's like WithSubject. The equivalent of Use in mfakes is called Inject in the RhinoAutoMocker class.

@AlexZeitler
Copy link
Author

Thx. Hope this is correct now. Now I run into the same issue as posted here: https://gist.github.com/923466.
Thought _mocks.UseConcreteClassFor may help, but the issue remains.

@BjRo
Copy link

BjRo commented Apr 18, 2011

You're on the way .. one slight modification though. This is how the creation should be triggered:

Because of
    = () => {
                _exception = Catch.Exception(() 
                    => _mocks.ClassUnderTest);
    };

@AlexZeitler
Copy link
Author

This leads to 'Only assignment, call, increment, decrement, and new object expressions can be used as a statement'

@BjRo
Copy link

BjRo commented Apr 18, 2011

Sorry, of course ...

Because of = () => {
_exception = Catch.Exception(() => _modelReader = _mocks.ClassUnderTest);
};

@AlexZeitler
Copy link
Author

Updated to:
Because of
= () =>
{
ModelReader modelReader;
_exception = Catch.Exception(()=> modelReader = _mocks.ClassUnderTest);
};

Initial issue remains

@BjRo
Copy link

BjRo commented Apr 18, 2011

Yes of course. But this is how the SM AutoMocker is intended to be used ;-)

@AlexZeitler
Copy link
Author

;-)
Thought UseConcreteClassFor should fix :(
http://jcselke.blogspot.com/2010/04/konkrete-klassen-mit-dem-structuremap.html
Sort of show stopper at the moment... COM sucks.

@BjRo
Copy link

BjRo commented Apr 18, 2011

Does this work? Or same error?

var container = new Container(x => x.For<ISldWorks>().Use(() => new SolidWorksOleConnector().Connect(new SolidWorksVersion2010()));
var reader = container.GetInstance<ModelReader>();

@AlexZeitler
Copy link
Author

the snippet had some errors, modified it this way to make it run:
_container = new Container(x => x.For<ISldWorks>().Use(() => new SolidWorksOleConnector().Connect(new SolidWorksVersion2010())));

Because of
= () =>
{
_exception = Catch.Exception(()=> _modelReader = new ModelReader(_container.GetInstance<ISldWorks>()));
};

All tests green now.

@BjRo
Copy link

BjRo commented Apr 18, 2011

Then this is your lucky day. This will be the default model in the next version of mfakes which will be released this week ;-)

@AlexZeitler
Copy link
Author

fixed the Because to:
_exception = Catch.Exception(()=> _modelReader = _container.GetInstance<ModelReader>());

still green.

@AlexZeitler
Copy link
Author

w00t! ;-) Thank you, mate!

@AlexZeitler
Copy link
Author

Something went wrong:

public class When_putting_a_concrete_solidworks_instance_into_a_mock : WithSubject<ModelReader> {
static ISldWorks _solidWorks;
static IModelDoc2 _model;
static Exception _exception;
static ModelReader _modelReader;

Establish context
    = () =>
        {
            Configure(config =>
                        config.For<ISldWorks>()
                            .Use(new SolidWorksOleConnector()
                                .Connect(new SolidWorksVersion2010())));
        };

Because of
= () => {
        _document = Subject.Document;
    };

It should_return_the_active_modeldoc
    = () => { _modelReader.Document.ShouldNotBeNull(); };

static IModelDoc2 _document;

}

throws the exception again...

@BjRo
Copy link

BjRo commented Apr 18, 2011

Use the lambda based factory as in the SM example. This should work

Configure(config =>
                    config.For<ISldWorks>()
                        .Use(() => new SolidWorksOleConnector() //<! ----------------------
                            .Connect(new SolidWorksVersion2010())));

@AlexZeitler
Copy link
Author

sorry, missed that due to hurried work ;-)
works like a charme now - thanks again.

@BjRo
Copy link

BjRo commented Apr 18, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment