Is there an example someone could point me to wher...
# box-products
a
Is there an example someone could point me to where WireBox is used for DI within TestBox? I have tried following the docs but I keep running into an error: "The DSLNamespace: coldbox cannot be used as it requires a ColdBox Context" FYI: I am aware that using DI in unit tests is not best practice, but I would like to in this context because I would have to mock a ton of dependancies and I just don't want to.
I have mapped the /models dir in my coldbox app to /model in application mappings, it seems like wirebox doesn't like the mapped path. All my injected dependancies use the id:model.myComponentName syntax. How do I get wirebox to accept the mapping?
b
What is your base test case?
a
This is in my testbox application.cfc:
this.mappings[ "/model" ] = expandPath("/models");
application.wirebox = new coldbox.system.ioc.Injector('coldbox.system.ioc.config.Binder');
application.wirebox.getBinder().mapDirectory("/model");
writeDump(application.wirebox.getBinder().getMappings());
writeDump(application.wirebox.getInstance("models.userInbox.UserInboxService"));
abort;
I
When I use the actual dir name in mapDirectory("/models") I get a little further until the object I am trying to get an instance of tries to autowire a dependancy that is declared like so:
inject="id:model.userInbox.userInboxGateway"
So its definitely the mapping that is the problem here.
Wirebox works fine in my ColdBox application, but I'm having this difficulty only when trying to introduce WireBox in TestBox
b
Did you see my question above??
Also, you've given two different errors/examples so I'm not sure which one you want help with
The ColdBox DSL one, or the model one...
a
model one is what seems to be the actual problem.
b
picking up kids from school.... I'll be back in a while
First note-- you seem to be using a
model
folder, but it's been
models
for quite a few years now 🤔
a
Do you think best scenario is to abandon the mapping and go with the default /models?
b
Well, I'd recommend it, but I'm not sure it has anything to do with our issues
Copy code
application.wirebox = new coldbox.system.ioc.Injector('coldbox.system.ioc.config.Binder');
application.wirebox.getBinder().mapDirectory("/model");
☝️ That's going on here? This isn't something you should need to be doing
Also, I'm still waiting for you to tell me what your base test case is
And I'm also still unclear on specifically what your test looks like and what exact code is failing.
a
Sorry, had to restart. I'm not sure what you mean by base test case. I was trying to get an instance of a component right in the application.cfc file and hadn't tried this in a test template yet.
b
Why are you trying to get CFC instances in your appliation.cfc?
By base test case I mean your actual spec
Copy code
component extends="testbox.system.BaseSpec" {
or whatever
a
I have my /tests folder that is running the TestBox browser test runner
b
There are a bunch of base classes you can use
a
So I was trying to get the DI running for those tests and figured I needed to set that up there.
b
No, that's now how to do it
If you want to write an integration test, you use one of the ColdBox base test cases
a
Is there an example of how to do that and use WireBox to instantiate the component for testing with all its dependancies?
b
It has all the plumbing to create ColdBox with all your settings including WireBox and your modules, etc, etc inside of the test
The only ting you should be doing in your application.cfc is bringing over any necessary • orm settings • CF mappings • dataources/caches/etc that already exist in your main app that is needed for your code to run.
a
Ok, I'll gut out the stuff I added. I'll take a look at these docs and give it a shot. I was probably trying to do this the hard way.
b
If you set
this.loadColdBox=true
then you can autowire your model that's created by the base test case or just ask for a fully-wired versioon with
Copy code
getInstance( 'model' )
just like if you were in your main app
a
This is the BaseSpec I was using to test my component. I don't want to have to mock out all my dependancies so I want to be able to just create that in the beforeAll()
b
Though, if you're going to use
getInstance()
to get your wired model, then you don't really need the basemodeltest. You can just use
coldbox.system.testing.BaseTestCase
directly which loads ColdBox by default
Right, so • line 7 is extending the wrong class • Line 11 is unnecessary (testbox does this for you) • line 12 is wrong, ask WireBox for the instance, don't create it direcly
Also, your
beforeAll()
runs once, but your
beforeEach()
runs before each spec which effectively means that data will never be there
a
Oh man, I've been doing testbox all wrong all this time
Is it normal to have to create a mapping to the testbox folder in order for the test browser to work?
b
Depends on how your web root is setup. If testbox is not in the web root, then yes a mapping would be required