Rhino Mocks - Private Properties On Ctor Test - rhino-mocks

i have a very simple question , i'm new to rhino
this is my class:
i want to test the property with
Expect.SetPropertyWithArgument
can anyone show me how, cuase i tried and its not working..
public class person
{
public string Name {get;private set;}
public person(string name)
{
Name = name;
}
}

The point of Expect.SetPropertyWithArguments is that a property is set on the mocked object, not on the object under test. I'd also point out that the code you've shown doesn't set a property at all - it's setting a field.

Related

WPF: Properties with ReadOnly attribute not being XAML serialized

I'm working on a C# WPF application that makes use of a Windows Forms PropertyGrid to allow the user to view and change an object's properties. Some properties I want to be visible but locked, so I'm setting their ReadOnly attribute to true.
However, elsewhere in the project we're using the XAML serializer to serialize objects and have found that the properties with the ReadOnly attribute set are being omitted from the serialization.
To demonstrate this:
// Simple class containing 3 properties, one of which has a ReadOnly attribute
public class TestClass
{
public int a { get; set; }
[ReadOnly(true)]
public int b { get; set; }
public int c { get; set; }
}
Running this code:
TestClass test = new TestClass();
test.a = 1;
test.b = 2;
test.c = 3;
string xStr = XamlWriter.Save(test);
Console.WriteLine(xStr);
gives the output:
<TestClass a="1" c="3" xmlns="clr-namespace:myTest;assembly=myTest" />
Clearly the 'b' property is missing.
Is this correct behaviour? Is it possible to serialize properties that have ReadOnly set to true?
If you are happy that your output may not be deserializable then you can achieve what you want by also decorating your [ReadOnly(true)] property with:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

mono rises unexpected compiler errors

I have the following class
public class LockRequest
{
public int Id { get; set; }
public string TypeName { get; set; }
public bool Ok { get; set; }
public LockRequest ( int id, string t)
{
Id = id;
TypeName = t;
}
}
Then, it's referenced in a delegate, as follows
private static void ReceiveLockRequest<LockRequest>(PacketHeader header, Connection connection, LockRequest input )
{
LockRequest lr = new LockRequest(1, "SomeTypeName" );
Console.WriteLine( String.Format ( "{0} ", input.TypeName) );
}
When compiling, both lines from the delegate rises compiler errors.
The line with the "new()", produces "Cannot create an instance of the type class 'LockRequest' because it does not have the 'new()' constraint.
The line which would show some of the input data gives "The type 'Lockrequest' does not contains a definition for 'TypeName' and no extension method 'TypeName' ... etc".
Could someone explain why is this behaviour?
My dev environment is Ubuntu 10.04 (64 bits) and Monodevelop 2.8.6.3
TIA
Could add some info.
I changed the name of the class, and the thing compiled. The whole class is to be serialised by ProtoBuf, so it must be decorated with attributes. Here are is a sample
[ProtoContract]
public class Foo
{
[ProtoMember(1)]
public int { get; protected set; }
[ProtoMember(2)]
public string TypeName { get; protected set; }
...
Just after I added the attributes, mono stop compiling. Same erors raise again.
To test it, I commented the attributes, do a Clean All, an recompile. The errors raise again, as if MonoDevelop cached them.
I need some help more than after the initial post.
2013-10-31
Thank you, Jester. It´s an event handler, from NetworkCommDotNet library.
My faults:
1) The first error (members not recognized) raises from the fact that (somewhat astobishing) the "input" argument comes as a plain object. Casting it in another method does the trick.
2) The error regarding the instanciation: the delegate definition in the library have a where clause wich states that T must be class, but no the new() constraint.
That's not a delegate, that's a generic method.
It's not clear what you want to do and why do you need a generic method.
If you really do, then try something along the lines of:
private static void ReceiveLockRequest<T>(PacketHeader header, Connection connection, T input) where T:LockRequest
PS: your development environment is very old, consider upgrading.

Object reference not set to an instance of an object, Interface

I know that this is one of the most encountered error but I am really struggling to get around it.
i have a property on my Controller:
private readonly ISelectListFactory _selectFactory;
and a method that called to populate the viewbag
private void PopulateLists()
{
var continent = _selectFactory.GetItems();
}
and the interface
public interface ISelectListFactory
{
IEnumerable<SelectListItem> GetItems();
}
and in the controller constructor I have the following:
public LocationController(ISelectListFactory selectFactory)
{
_selectFactory = selectFactory;
}
but I am getting this error Object reference not set to an instance of an object and not sure how to overcome it.
Regards
Make sure you have instantiated this _selectFactory variable somewhere. Like for example:
_selectFactory = new SomeConcreteSelectListFactory();
or if you are using dependency injection you might configure your DI framework to inject it into the constructor:
public class HomeController: Controller
{
private readonly ISelectListFactory _selectFactory;
public HomeController(ISelectListFactory selectFactory)
{
_selectFactory = selectFactory;
}
... some controller actions where you could use the _selectFactory field
}

Fluent NHibernate ShouldMap does not detect my custom attribute

I have been spending a couple of days now to get to know the Fluent NHibernate automapping working model. It is quite nice, but I keep detecting new details missing from my schemas. Now I want to add extra properties to my classes, but not have them mapped to the database. A typical case is when I need extra properties with internal logic.
So I read the examples and scanned StackOverflow and found out that this was not another convention to be added, but rather a matter of inheriting the DefaultAutomappingConfiguration and override the ShouldMap method.
Fine, no problem, one minute later I had something like this:
public class CustomAutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
var explicitSkip = member.PropertyType.GetCustomAttributes(typeof(SkipMap), false).Length > 0;
if ((member.IsProperty && !member.CanWrite) || explicitSkip)
{
return false;
}
return base.ShouldMap(member);
}
}
/// <summary>
/// Don't map this property to database.
/// </summary>
public class SkipMap : Attribute
{
}
public class DemoClass
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual MyBitwiseEnum Status { get; set; }
public virtual bool IsValid
{
get
{
return (int)Status > 3;
}
}
[SkipMap]
public virtual bool IsBad
{
get
{
return MyBitwiseEnum.HasFlag(MyBitwiseEnum.Bad);
}
set
{
MyEnum = value ? MyBitwiseEnum | MyBitwiseEnum.Bad : MyBitwiseEnum ^ MyBitwiseEnum.Bad;
}
}
}
I know that my demo class is kind of stupid, but it will illustrate my point.
The idea is that I want to manually decide what properties to map to database.
The readonly property works fine because the ShouldMap method will look for property.CanWrite. But the custom attribute that definitely is set will not be detected. Why is that!?
In the convention methods I have used the same approach frequently and there it works fine. Why is the property not able to detect defined attributes here, when it obviously can in the convention setting. Is there a workaround?
have you added your new automapconvention to Automap?
AutoMap.AssemblyOf<>(new CustomAutomappingConfiguration())
Update: you are getting the skip attribute from Boolean class instead of the property
member.PropertyType.GetCustomAttributes(typeof(SkipMap), false)
should be
member.MemberInfo.GetCustomAttributes(typeof(SkipMap), false)
Just to be sure the custom attribute is applicable to properties, try adding [AttributeUsage(AttributeTargets.Property)] to your SkipMap class.
Another possibility is an attribute name clash with another attribute that applies to different targets. Try renaming the class to something like MyVerySpecialSkipMap and retest to verify you don't have an attribute clash. At the very least, write some simple reflection code to test for the SkipMap attribute outside the context of your application to ensure it can be found.

How to map private fields in FluentNHibernate using DefaultAutomappingConfiguration

The guidance given here http://fluentnhibernate.org/blog/2010/05/23/feature-focus-fields.html suggests that from V1.1 the automapping feature of Fluent NHibernate supports mapping to private fields.
So given the following code, NHiberate should be able to map to the myValue field.
public class SomeEntity
{
private string myValue;
public virtual int Id { get; set; }
}
public class DomainAutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(FluentNHibernate.Member member)
{
return (member.IsProperty && member.IsPublic && member.CanWrite) ||
(member.IsField && member.IsPrivate);
}
}
However when I run this code and try to map, I get the following exception:
NHibernate.PropertyNotFoundException : Could not find a getter for property 'myValue' in
class.....
I am using FluentNHibernate 1.1 and NHibernate 3.0.0.2001
What am I doing wrong?
Change:
private string myValue;
To:
private string myValue {get;set;}
I am not sure if this will do it for you, but the error you are receiving is the lack of the {get;} when designating the private field. Hopefully this will put you on the right track. I have not tried mapping private fields.
Good luck.