Is there a way to populate an object instance using AutoFixture? - autofixture

Here is a sample of what I'm trying to do
var foo = CreateFoo(); // I don't have access to the internals of CreateFoo
fixture.Populate(foo); // foo should now have random property values
Obviously, there is no Fixture.Populate method. But how should I solve this problem?
Thanks!

AutoFixture indeed doesn't provide API to fill writable properties of an existing object. However, you can use the following snippet to achieve the desired behavior:
var fixture = new Fixture();
var objectToFill = new TypeWithProperties();
new AutoPropertiesCommand().Execute(objectToFill, new SpecimenContext(fixture));
Assert.NotNull(objectToFill.Value);
If you need that often, you can create an extension method for the IFixture in your test project.

Related

Serializing objects Codename One

How do I serialize an object in order to make a customizable Parse initialization? Like:
//Simple text fields to get info
TextField url = new TextField();
TextField appid = new TextField();
TextField clientkey = new TextField();
then I put all in an object e.g:
Myclass object = new Myclass();
object.url = url.getText();
object.appid = appid.getText();
object.clientkey = clientkey.getText();
so I put it here, but before it needs to be serialized in order to keep its values after my app get restarted.
//After serialization
Parse.initialize(object.url, object.appid, object.clientkey);
In this way I can set my Parse initialization by my application instead.
I'd appreciate to see an example of serialization in this case.
When you store an object in parse it's saved locally so you don't need to serialize.
FYI Codename One supports the Externalizable interface to serialize objects in binary form. It also supports seamless externalization for object properties. The latter don't work with Parse AFAIK.
There's no support for serialization. You're on your own.

How do you use an Event, given only a name string supplied in xaml?

I've seen in triggers, attached properties, and behaviors the ability to specify SourceObject="myButton" EventName="Click" -- what I want to know is how do you access the event, given its name and owner object, in code? Does this require reflection to implement?
Yes, you use reflection:
var instance = new SomeClassType();
var method = "MyEvent";
var handler = instance.GetType().GetMethod(method);
handler.Invoke(instance, null);

Why can't I add items to an array if it is inside a singleton class?

I have a singleton class which looks like this:
class DataAccess {
var teamUrl = NSArray()
}
let sharedData = DataAccess()
This is saved as a .swift file.
I am trying to add an item to that array from one of my view controllers. The code I am using to try and add the item to the array is this:
sharedData.teamUrl.append(teamUrl.text)
I am receiving an error message though. The error message is:
'NSArray' does not have member names 'append'
I know that it is something to do with my singleton because I created an array in the view controller and changed the code correspondingly and it worked.
I also tried to change the Singleton class to this:
class DataAccess {
var teamUrl = []
}
let sharedData = DataAccess()
I received the same error though.
I'm not sure why this isn't working. Please would somebody be able to explain where I have gone wrong?
Just to add, although I have referred to these as singletons, I'm not 100% sure that they are - its just the name they were referred to on a tutorial when I was learning about transferring data between different views.
Instead of var teamUrl = [], try var teamUrl: [String] = []. This will create a native Swift array, instead of an NSArray, and that has a .append method.

RegisterMultiple does not keep implementation type as singleton if registered as multiple registration types?

I am trying to add a type called TypeA as two different registration types: InterfaceA and InterfaceB.
container.RegisterMultiple(typeof(InterfaceA), new[] {typeof(TypeA), typeof(TypeB)});
container.RegisterMultiple(typeof(InterfaceB), new[] {typeof(TypeA), typeof(TypeC)});
But when I Resolve them, I get one instance of TypeA when resolving InterfaceA, and another instance when resolving InterfaceB. I expect to get the same instance for both resolves, but I am not.
I have also tried to add .AsSingleton() to the call, but it made no difference.
Am I doing something wrong, or does anyone have any ideas of doing this without adding a TypeAFactory or such that keeps track of the instances instead?
Thanks in advance for any help.
I think what you are seeing is by design.
To get the same instance for both interfaces you can create the instance yourself and register it for both interfaces:
var instanceOfA = new TypeA(...);
container.Register<InterfaceA>(instanceOfA);
container.Register<InterfaceB>(instanceOfA);
I solved this on my own with a quite ugly (but yet quite elegant) solution.
I create another, internal, instance of a TinyIoCContainer, and I register all my types with the actual TinyIoCContainer.Current by giving it a factory in the form of:
var container = TinyIoCContainer.Current;
var internalIoC = new TinyIoCContainer();
Dictionary<Type, Object> instances = new Dictionary<Type, Object>();
...
Func<TinyIoCContainer, NamedParameterOverloads, Object> factory = (TinyIoCContainer c, NamedParameterOverloads o) =>
{
if (instances.ContainsKey(implementationType) == false)
{
// Create the instance only once, and save it to our dictionary.
// This way we can get singleton implementations of multi-registered types.
instances.Add(implementationType, internalIoC.Resolve(implementationType));
}
return instances[implementationType];
};
container.Register(registerType, factory, implementationType.FullName);
I'm sure there will be a few caveats with this solution, but I'm also sure I'll be able to figure out a workable fix for them, as things look right now.

Creating Test EntityList objects - RIA Services

I'm creating an EnityList to do some client side testing with my ViewModel. Something like:
var people = new EntityList<Person>()
{
new Incident() {Age = 55, Name="Joe"},
new Incident() {Age=42, Name="Sam"}
};
The problem is that the implicit (and explicit) Adds fail. The entitylist is created as read-only. Any thoughts on how to create a test EntityList?
If you're doing testing then you probably don't want an EntityList. I would expect that a ViewModel shouldn't know about the EntityList, but rather should just get access to an IEnumberable instead. Both EntityList and List expose this, so in your tests you can just create a List.
I realize this doesn't help with the issue of EntityList being read-only. :)
I think you also need an EntityContainer to own your EntityList.

Resources