-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem mocking class types for variable declarations #90
Comments
Okay, so this was be being a noob :) I got this working. Brought out the old C# knowledge and got this working. Sorry friends. |
@johlju Check out the SharePointDSC resource as this is one where they do a lot of mocking within the DSC tests for on the Appvayor builds where SharePoint won't be installed either. I would also recommend getting in touch with @BrianFarnhill for more background questions as I know that SharePoint has alot of similar reliance on the object types as SQL will do too though I know in SharePointDSC they don't mock the object the same way as you are looking to do so. |
I can definitely help with this - I wrote a blog post about how we did it for SharePointDsc - happy to get involved here to help implement something similar for xSqlServer as well (I'm working with a colleague on contributing to this resource at the moment anyway!) |
Thanks for all the feedback! Really appreciate how we help each other out. @BrianFarnhill i checked out your blog post and also glanced at the SharePointDsc resource, and the way you did stubs. Unfortunately in SQL Server we are lacking a lot of cmdlets. What we do have is the 'SMO model'. The .net classes with namespace Microsoft.SqlServer.Management.Smo. I am using a stub file, just like SharePointDsc, but the stub file is a C# file Appreciate more feedback on this. If there is any way to do this even better, lets make those changes. |
Reopen this issue for while so we can discuss this. |
Was thinking when I coded the mock classes that looping thru entire 'SMO model' and build mock classes and enums might be helpful. These could then be improved with necessary logic. |
Yea so with SharePointDsc we've done a few things when we need to implement specific objects as opposed to mocking PowerShell cmdlets. The first is that we do the Add-Type thing with some stubs for some of them where we needed an object that we don't really care what we do with it - like our search topology ones, we need an object with a specific type name/namespace but other than that its just properties on an object, so we mock those out like this:
That sort of thing we could automate the generation of pretty easily - load the required assemblies then get objects and namespaces and iterate through those. I would be saving those out to a .cs file and then importing that in rather than coding them in directly like we do here. The second thing we do where we need to return specific things from methods on those objects is spin them up in PowerShell like this:
Now the benefit here is that we can control in PowerShell what gets returned from specific methods, which is a plus. We can also do things like set global variables inside the ScriptMethods so we can poll those to check that the methods were called (because we can't do an Assert-MockCalled on those). Now we could come up with some approaches like this that would work as well assuming you don't rely on the GetType() method in your code at all (because the type will be different), but it would be more complex to generate these. Currently we write these up by hand in our mocks (because we also want them to return different things under different contexts) and I would recommend that the same approach be taken here for the places that you need them. Things like enums are a no brainer - we can script those up to a cs file and import those, but I think we need to do some careful thinking about how you want to implement the other objects that you will actually interact with in your tests to make sure you set yourself up for better success. |
Okay so you just use [Object] instead of the actual class names. Do you ever use any classes with their namespace as types?
How would you go about this? Mock the type using To be able to 'mock the right state' I had to make a constructor that sets a variable to remember the state, see
Ps. I would have loved to use PowerShell classes if they supported namespaces. That would have been awesome to get C# like coding and PowerShell efficiency at the same time. Ds. |
@BrianFarnhill I generated stub files for SQLPS and SQLServer modules using the code on your blog. I change it a bit to get the formatting as close as possible. The stub files are included in the PR #98. Modified "helper" function to generate stub module file:
|
Closing this. I think it is obsolete now. We are long past this now. :) Thanks all! |
I need to figure out if this is possible to accomplish. Any comments appreciated. Trying to mock a DSC Resource on a system that does not have SQL Server. Doing that I came across this problem that I have a used specific types for variables. Like the example below, it uses the type
Microsoft.SqlServer.Management.Smo.PermissionSetBase
.I tried to solve that by using a class and adding that as a type (below example is simplified).
But this does not work since that type will only exist during runtime, not during parsing, giving me syntax errors (missing types in variables declarations).
Adding it as a PowerShell class does not work either since it classes does not support namespaces.
The only way I see past this is to use
[Object]
. But would really like to keep the types as-is.@mbreakey3 do you have any feedback on this?
The text was updated successfully, but these errors were encountered: