Autofixture: Using AutoMoqCustomization & SetupAllProperties to have all properties of a mocked interface fillled? - moq

I am trying to get Autofixture to setup and create me an anonymous of an interface. I am using the AutoMoqCustomization, but I keep getting an error.
My code is
var configuration = fixture.CreateAnonymous<Mock<IConfiguration>>();
Mock.Get(configuration).SetupAllProperties();
It actually errors on the SetupAllProperties with
System.ArgumentException : Object instance was not created by Moq.
Parameter name: mocked
Anyone know what I am doing wrong?

You're trying to get a Mock<IConfiguration> from a Mock<IConfiguration> instance, which is hardly necessary. Just use
var configuration = fixture.CreateAnonymous<Mock<IConfiguration>>();
configuration.SetupAllProperties();

Related

Symfony2 use Doctrine in ExceptionController

Hi I'm trying to use Doctrine inside the default ExeptionController but I get the following error:
Fatal error: Call to undefined method Symfony\Bundle\TwigBundle\Controller\ExceptionController::getDoctrine()
when I try to call:
$manager = $this->getDoctrine()->getManager();
What I'm trying to do is to have a custom 404 page where I can present some items from the database.
Could you please help me? Thank you!
You may also inject the Doctrine service as a dependency in your Controller (in that case you don't need to entend class Controller)
You will have to create your own ExceptionController extending the default one. You'll have to declare it as described here: http://symfony.com/doc/current/cookbook/controller/error_pages.html#custom-exception-controller. Your custom controller must have a constructor with at least an argument of type Registry (Doctrine). You have to declare that controller as a service in your service.yml (or xml depending on your config) Have a look at the symfony doc for further explanation on how to do that. For the moment I can't help you much more as I'm outside with my Android and it's rather difficult to make long answers

SPDispose Ignore attribute not ignoring

In my SharePoint code, I have the following line:
SPWeb web = site.RootWeb; //site is an SPSite object
When I rebuild my project and run the SPDispose tool on the assembly, I get the following error:
Module: Blah.SharePoint.Features.Core.dll Method:
Blah.SharePoint.Features.Core.Helpers.FeatureDeploymentHelper.RemoveWebPartFiles(Microsoft.SharePoint.SPFeatureReceiverProperties,System.String)
Statement: web := site.{Microsoft.SharePoint.SPSite}get_RootWeb()
Source:
C:\xxx\xxx\Main\Source\SharePoint\Features\Core\Helpers\FeatureDeploymentHelper.cs
Line: 26
Notes: Disposable type not disposed: Microsoft.SharePoint.SPWeb
***This may be a false positive depending on how the type was created or if it is disposed outside the current scope More Information:
http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_140
What I want to do is to have the SPDispose tool ignore this error, so I have pulled the SPDisposeCheckIgnore class and supporting enum into my project, and I've decorated my method appropriately:
[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_140, "RootWeb does not need disposed.")]
public static void RemoveWebPartFiles(SPFeatureReceiverProperties properties, string assemblyName)
{
...
}
After doing all of this, I still receive the error. Anyone know how I might go about getting rid of that error?
Two things need to be done here.
1) The SPDisposeCheckIgnore class must be defined in the SPDisposeCheck namespace. You CANNOT have your own namespace. See the related comment on this page: http://archive.msdn.microsoft.com/SPDisposeCheck/
2) Anything you are trying to ignore within RunWithElevatedPrivleges must be pulled into an external method or it will not be recognized. This was not being done in the example above, but was being done in other places.
These two rules must be followed for ignore to work. Hope this helps someone else down the road.
Double check how you're retrieving and assigning the RootWeb object. If it's done in an external method, the DisposeChecker might not pick up that it's a RootWeb reference.
I don't see anything wrong with what you've written. I pulled out getting the root web into a static method so I only had to ignore this error in one place, and it works in anonymous delegates. The following worked for me:
public class DisposeUtils
{
[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_140, "RootWeb does not need disposed. http://blogs.msdn.com/b/rogerla/archive/2009/11/30/sharepoint-2007-2010-do-not-dispose-guidance-spdisposecheck.aspx")]
public static SPWeb GetRootWeb(SPSite site)
{
return site.RootWeb;
}
}
Sorry, I'm not sure if that helps exactly - I'm saying your code should work. Have you checked SPDisposeCheck to see how it handles 'Documented' and 'Undocumented' errors? (I've never been entirely clear what those settings do)

Symfony2 Set Controller in the kernelControllerEvent using bundle:controller:action notation

I am trying to do something like the following question:
Trying to swap a controller using an event listener with Symfony2
However, when I use the code (as recommended in the answer):
$event->setController('MyMainBundle:Manage:show');
I just get an error:
LogicException: The controller must be a callable (MyMainBundle:Manage:show given).
Is there a way to use that Bundle:Controller:Method syntax in setController? Or maybe some other method I can call to resolve that to a "callable"?
What you should give to $event->setController is a callable.
What you give a string representing the logical path to a callable.
You can resolve this string using symfony's ControllerResolver.
You have to inject the controller_resolver service in your listener, and then use it like this:
$request = new Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'MyMainBundle:Manage:show'));
$event->setController($this->resolver->getController($request));
But you are clearly doing the framework's job here.

how can i use structure map asp.net 3.5

I am new to the structure map but i want to use it in my asp.net site for dependency injection
can any one suggest me simple example to use structure map for the dependency injection
you will need to do something like this:-
StructureMapConfiguration
.ForRequestedType<IResourceA>()
.TheDefaultIsConcreteType<ResourceB>()
.CacheBy(InstanceScope.Singleton);
This tells StructureMap to inject ResourceB when there is a request for ResourceA.
Structure Map
You can configure programatically or via configuration file.
Programatical example (there are other ways):
StructureMap.StructureMapConfiguration.ForRequestedType<ISomething>().TheDefaultIsConcreteType<ConcreteSomething>();
then you can get an instance of the configured type using this sort of code:
//The concrete type will be ConcreteSomething
ISomething instance = ObjectFactory.GetInstance<ISomething>();
You can do it in a config file:
<StructureMap MementoStyle="Attribute">
<DefaultInstance PluginType="Blah.ISomething, Blah.SomethingDLL" PluggedType="Blah.Concrete.ConcreteSomething,Blah.ConcreteDLL"/>
</StructureMap>
and in the main method or Global.asax you can set this config by saying:
StructureMap.ObjectFactory.Initialize(x => { x.PullConfigurationFromAppConfig = true; });
and use it the same way as above:
ISomething instance = ObjectFactory.GetInstance<ISomething>();
If the concrete class has a constructor that needs instances injected in it, and you have those configured, the concrete types will get injected by the framework.
There are ways of passing parameters to constructors, dealing with Gereric types, creating named instances that are configured with specific constructor/property values. I use this framework and like it very much.

Flex & WCF - Enum error

I have a WCF service operation that accepts a data contract parameter of custom type MyQuery -- function Search(q as MyQuery). The MyQuery object contains 2 properties:
MyQuery.SearchPhrase (string)
MyQuery.SearchType (custom enum SearchTypeEnum)
I also have a Flex client application that consumes this service. But when Flex invokes the Search() operation, I get the following error about the enumeration property:
"Cannot find definition for type
'http://mydomain/2009/04/SearchTypeEnum::SearchTypeEnum"
The error is thrown from Flex while it is building the request to the service.
So my question is..... is there any way to work around this issue in Flex? Or is my only alternative to redesign the service without enums?
This is how the enum data contract is defined at the service tier:
<DataContract(Namespace:="http://mydomain/2009/04/SearchTypeEnum")> _
Public Enum SearchTypeEnum
<EnumMember()> [Boolean] = 0
<EnumMember()> [NaturalLanguage] = 1
End Enum
As far as I know this is the correct definition of an enum data contract. I also tried using the ServiceKnownType and KnownType attributes on the service and on the MyQuery class but to no avail.
I would recommend you try that with a trivial .NET client - maybe even a console application. The idea would be to see if you can get any application to work with the service.
I could not find a way to get this to work. Instead I replaced enums with string constants. Not as elegant, but it works.

Resources