ExpectedObjects compares concrete objects instead of only interfaces - c#

I have an xUnit unit test project utilizing ExpectedObjects. Right now I'm trying to test a service that returns objects conforming to an interface. For the sake of the question let's take an example interface
public interface INamed
{
string Name { get; }
}
and two different implementations:
public class ComplexEntity : INamed
{
public int Id { get; set; }
public string Name { get; set; }
public string Domain { get; set; }
/* Many other properties follow... */
}
public class SimpleEntity : INamed
{
public int Id { get; set; }
public string Name { get; set; }
}
The method under test has a signature of
public IEnumerable<INamed> GetNames();
All that I care about is that the correct names are returned. So after mocking whatever dependencies the service might take I construct an expected result set as follows:
IEnumerable<INamed> expected = new []
{
new SimpleEntity { Name = "NAM1" },
new SimpleEntity { Name = "NAM2" },
...
}
And I compare them as follows:
// ACT
var result = systemUnderTest.GetNames();
// ASSERT
expected.ToExpectedObject().ShouldMatch(result);
This will fail. The collection that is actually returned by the GetNames under test is a mixed collection of both SimpleEntities and ComplexEntities, and none will match, because the Id properties of returned objects are non-zero, and other properties from ComplexEntity that I don't care about are not even present in the expected set.
The ExpectedObjects docs give me no guidance on this. The ShouldMatch is supposed to work on anonymous types, so this should (and does) work:
expected.Select(e => new { e.Name }).ToExpectedObject().ShouldMatch(result);
But I see this as an inflexible workaround. There are many other methods with contracts including only interfaces where I don't care about the underlying types. I would have to manually select the same properties from an interface in every such test and if that interface ever changed to include more properties the tests would still pass even though they wouldn't be correctly checking the contract. For example if I added the Id property to INamed, the test would still pass and would never even test for Id, allowing errors to go on silently.
Is there an out-of-the-box way to make ExpectedObjects compare only the public interfaces? I guess I could manually write a contrived, dynamic method creating an anonymous type out of an interface, make it generic and roll with it, something with usage like:
expected.ToExpectedObject().ShouldMatchInterface<INamed>(result);
but that makes me question using the library in the first place, as the effort put into ShouldMatchInterface would probably be significant.

It doesn't look like you're setting up the Expected Object correctly.
For example, with a GetNames() implementation like this:
public class Foo
{
public IEnumerable<INamed> GetNames()
{
return new[] {
new ComplexEntity() { Id = 1, Name = "NAM1", Domain = "DOM1" },
new ComplexEntity() { Id = 2, Name = "NAM2", Domain = "DOM2" }
};
}
}
The following xUnit + ExpectedObjects test will pass:
using ConsoleProject;
using ExpectedObjects;
using Xunit;
namespace ConsoleProject_Tests
{
public class ExpectedObjectsTests
{
[Fact]
public void GetNames_should_return_INamed_ExpectedObjects_Style()
{
var expected = new[]
{
new { Name = "NAM1" },
new { Name = "NAM2" }
}.ToExpectedObject();
var systemUnderTest = new Foo();
var actual = systemUnderTest.GetNames();
expected.ShouldMatch(actual);
}
}
}
Note that ToExpectedObject() is being fed anonymous objects, not concrete classes.
Now compare that with the old way of doing things by implementing a custom IEqualityComparer<INamed> which just happens to be on the test class...
using ConsoleProject;
using System;
using System.Collections.Generic;
using Xunit;
namespace ConsoleProject_Tests
{
public class ClassicXUnitTests : IEqualityComparer<INamed>
{
bool IEqualityComparer<INamed>.Equals(INamed x, INamed y)
{
if (x == null && y == null) return true;
if (x == null || y == null) return false;
return String.Equals(x.Name, y.Name);
}
int IEqualityComparer<INamed>.GetHashCode(INamed obj)
{
return obj.Name.GetHashCode();
}
[Fact]
public void GetNames_should_return_INamed_xUnit_Style()
{
var expected = new[]
{
new SimpleEntity() { Name = "NAM1" },
new SimpleEntity() { Name = "NAM2" }
};
var systemUnderTest = new Foo();
var actual = systemUnderTest.GetNames();
Assert.Equal<INamed>(expected, actual, this);
}
}
}
It still needs a concrete class that implements INamed because you can't just create a new abstract class or interface.

Related

How can I use a dynamic to find out when a property is used?

I would like to find out which of the properties in a source input object, a method has used. After executing the method I need to store in a database which of the properties was used.
The input could be any class with simple types, like this:
public class MyData : IMyData
{
public string A { get; set; }
public int B { get; set; }
public decimal C { get; set; }
}
I thought it could be done using an interface as input to the method, so I can replace the original object with a more advanced object, which stores usage of properties
public interface IMyData
{
string A { get; }
int B { get; }
decimal C { get; }
}
I can then
Create a dynamic object with the same properties
Use ImpromptuInterface to simulate the dynamic object implements my interface
Call my method with this dynamic interface
private static void Main()
{
var data = new MyData { A = "Test", B = 3, C = new decimal(1.2) };
IDictionary<string, object> replacementObject = new ExpandoObject();
replacementObject.Add("FieldsUsed", new List<string>());
foreach (var property in data.GetType().GetProperties())
replacementObject.Add(property.Name, property.GetValue(data));
var replacementInterface = replacementObject.ActLike<IMyData>();
DoStuff(replacementInterface);
Console.WriteLine($"The method used these fields {string.Join(", ", (List<string>)replacementObject["FieldsUsed"])}");
}
private static void DoStuff(IMyData source)
{
Console.WriteLine($"A is {source.A}");
if (source.B > 5)
Console.WriteLine($"C is {source.C}");
}
In the above example I would like to store that fields A and B have been used.
Only I am stuck at how I should store when a property is used by my DoStuff method.
You can write a wrapper like this:
public class ClassWrapper<T>: DynamicObject where T:class
{
private readonly T _obj;
private readonly List<string> _fieldsUsed=new List<string>();
public ClassWrapper(T obj)
{
_obj = obj;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
PropertyInfo propertyInfo = _obj.GetType().GetProperty(binder.Name);
_fieldsUsed.Add(binder.Name);
result = propertyInfo.GetValue(_obj);
return true;
}
public List<string> GetFieldsUsed() => _fieldsUsed;
public T GetWrapper()
{
return this.ActLike<T>();
}
}
and use it like
var data = new MyData { A = "Test", B = 3, C = new decimal(1.2) };
var mc=new ClassWrapper<IMyData>(data);
IMyData wrapped = mc.GetWrapper();
DoStuff(wrapped);
Console.WriteLine($"The method used these fields {string.Join(", ", (List<string>)mc.GetFieldsUsed())}");
If you want to know when a property is used, a Interface like INotifyPropertyChanged can do that for you at runtime. The exampel is only about notification for writes (that actually changed a value), but it would be trivial to expand it to reads and writes. It is not a perfect thing of course, as different executions might follow different code paths that use different properties.
If a function takes a specific type as input, you have to asume that all properties may be relevant. This is especially true for abstract types and interfaces - often the interface exists for this function. If it is one of those two, you can also always provide your own implementation of those Interfaces and Abstract class.
I can not shake the feeling that this is a XY problem.

c#: how to hide a field which is used only for XML serialization retrocompatibility?

The field is used only during the serialization / deserialization process but I would like to immediately encapsulate it and hide from the class.
Is it possible?
Basically, no.
XmlSerializer only works with public members, so you can't make it internal or private. You can add some attributes to make it less glaring especially in UIs that data-bind:
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public int Foo {get; set; }
but that only masks it. You could also look at IXmlSerializable, but that is a horrible API and most implementations of it are simply buggy - I do not recommend implementing this interface.
But: best practice is that whenever serialization requirements conflict with your model's design: create a dedicated DTO model - one that matches perfectly your chosen serialization library and exists purely for that purpose. And then map between the two. Then you don't have to compromise.
Its not possible with XML-Serialization in C# , if you want to do like that than you should make use of DataContractSerialization, It allows this kind of functionality i.e. you can serialize private field of you object.
Below is possible with DataContractSerialization, I hope you like to try out
[DataContract]
class Person
{
[DataMember]
public string m_name;
[DataMember]
private int m_age;
}
This what I tried when I was learning XML to Linq , and this is wired solution but if you want to try , here i created xml string by using xml to linq
here is my article : Object to XML using LINQ or XmlSerializer
Note : here code field of product class is private field but still you can generate xml string
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
class Program
{
public class Product
{
public Product()
{ }
public Product(string name,int code, List<productType> types)
{
this.Name = name;
this.Code = code;
this.types = types;
}
public string Name { get; set; }
private int Code { get; set; }
public List<productType> types { get; set; }
public string Serialize(List<Product> products)
{
XElement productSer = new XElement("Products",
from c in products
orderby c.Code
select new XElement("product",
new XElement("Code", c.Code),
new XElement("Name", c.Name),
new XElement("Types", (from x in c.types
orderby x.type//descending
select new XElement("Type", x.type))
))
);
return productSer.ToString();
}
}
public class productType
{
public string type { get; set; }
}
public static void Main()
{
List<productType> typ = new List<productType>();
typ.Add((new productType() { type = "Type1" }));
typ.Add((new productType() { type = "Type2" }));
typ.Add((new productType() { type = "Type3" }));
List<Product> products =new List<Product>() { new Product ( "apple", 9,typ) ,
new Product ("orange", 4,typ ),
new Product ("apple", 9 ,typ),
new Product ("lemon", 9,typ ) };
Console.WriteLine(new Product().Serialize(products));
Console.ReadLine();
}
}
Assuming you are using XmlSerializer, then only public fields and properties can be serialized, as explained in Troubleshooting Common Problems with the XmlSerializer:
The serializer examines all public fields and properties of the Type to learn about which types an instance references at runtime. It then proceeds to create C# code for a set of classes to handle serialization and deserialization using the classes in the System.CodeDOM namespace.
So, what are your options? If you are able to construct your XmlSerializer directly, you could make use of the XmlSerializer.UnknownElement event to forward the unknown elements to the object being deserialized for processing.
First, define the following attribute and extension methods:
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
public class XmlUnknownElementEventHandlerAttribute : System.Attribute
{
}
public static partial class XmlSerializationHelper
{
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
{
serial = serial ?? new XmlSerializer(typeof(T));
serial.UnknownElement += UnknownXmlElementEventHandler;
using (StringReader reader = new StringReader(xmlString))
{
return (T)serial.Deserialize(reader);
}
}
public static void UnknownXmlElementEventHandler(object sender, XmlElementEventArgs e)
{
var obj = e.ObjectBeingDeserialized;
foreach (var method in obj.GetType().BaseTypesAndSelf()
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
.Where(m => Attribute.IsDefined(m, typeof(XmlUnknownElementEventHandlerAttribute))))
{
method.Invoke(obj, BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { sender, e }, null);
}
}
}
public static class TypeExtensions
{
public static IEnumerable<Type> BaseTypesAndSelf(this Type type)
{
while (type != null)
{
yield return type;
type = type.BaseType;
}
}
}
Next, say you have some class like:
public partial class MyClass
{
public string MyValue { get; set; }
}
And some XML containing an element that needs to be post-processed and converted into the current model, e.g. <OldValue>:
<MyClass><OldValue>Hello</OldValue></MyClass>
Then add a method to MyClass that:
Can be private or internal (in full trust) or public;
Has the same signature as XmlElementEventHandler;
Is marked with your custom attribute [XmlUnknownElementEventHandler];
Performs the necessary post-processing on the old element.
And now the unknown element will be forwarded to it when using a serializer constructed by XmlSerializationHelper.LoadFromXml().
E.g., your method might look like:
public partial class MyClass
{
[XmlUnknownElementEventHandler]
void HandleOldElement(object sender, XmlElementEventArgs e)
{
if (e.Element.Name == "OldValue")
{
Debug.WriteLine("{0}: processed property {1} with value {2}", this, e.Element.Name, e.Element.OuterXml);
MyValue = "Old value was: " + e.Element.InnerText;
}
}
}
And you would deserialize as follows:
var model = xmlString.LoadFromXml<MyClass>();
One advantage of this solution is that it doesn't modify the XSD generated for your types in any way.
Sample fiddle. (Note that, because the dotnetfiddle code executes in partial trust, the handlers must be public. That's not necessary in full trust.)

MOQ returning dynamic types as object issue

pologise if this questions has been asked but I couldn't find the answer anywhere.
My problem is when mocking a return method using MOQ where that method returns a dynamic type. I'm using a third part library which uses dynamic times. MOQ seems to cast the dynamic type as object.
Mock<IFacebookHelper> mockFbHelp = new Mock<IFacebookHelper>();
mockFbHelp.Setup(x => x.Get("me")).Returns(new { email = "test#test.com", id="9999" });
Method in the mocked helper.
public dynamic Get(string p)
{
var client = new FacebookClient(AccessToken);
return client.Get("me");
}
Code from controller using mocked results.
_facebookHelper.AccessToken = accessToken;
dynamic result = _facebookHelper.Get("me");
int facebookId = int.Parse(result.id); //This errors as id doesn't exist.
Basically MOQ has returned a dynamic type of object that would require casting as something.
Does anyone know how to get around this problem? I'm assuming it may be because MOQ is not coded in .NET 4 therefore does not support dynamic types?
Edit
Actually I don't think this is a MOQ issue as I created my own mock class and still had the same problem. I'm new to dynamic types though so not sure what's going on.
Edit 2 - Part answered.. Problem nothing to do with MOQ after all
Actually the problem seems to be due to the dynamic type being created in a different assembly. Although I got round my initial problem using a JObject type I still want to figure this out.
namespace MyLib.Tools
{
public interface IDynTest
{
dynamic GetData();
}
}
namespace MyLib.Tools
{
public class DynTest : Effect.Tools.IDynTest
{
public dynamic GetData() {
return new { DynamicProperty = "hello" };
}
}
}
namespace Warrior.WebUI.Infrastructure
{
public class UseDynTest
{
private readonly IDynTest dynTest;
public UseDynTest(IDynTest dynTest)
{
this.dynTest = dynTest;
}
public string RetTest()
{
return dynTest.GetData().DynamicProperty;
}
}
}
namespace Warrior.Tests
{
[TestClass]
public class TestDynTest
{
[TestMethod]
public void TestMethod1()
{
//Mock<IDynTest> mockDynTest = new Mock<IDynTest>();
//mockDynTest.Setup(x => x.GetData()).Returns(new { DynamicProperty = "From Unit Test" });
DynTestProxy dynTestProxy = new DynTestProxy();
UseDynTest useTest = new UseDynTest(dynTestProxy);
string results = useTest.RetTest();
Assert.AreEqual("From Unit Test", results);
}
}
}
namespace Warrior.Tests
{
public class DynTestProxy:IDynTest
{
public dynamic GetData()
{
return (dynamic) new { DynamicProperty = "From Unit Test" };
}
}
}
There are 3 project indicated by the Namespace MyLib, Warrior.WebUI and Warrior.Tests.
As it is the test fails with an error..
'object' does not contain a definition for 'DynamicProperty'
which occurs on RetTest()
However if I simply move the DynTestProxy class into the Warrior.WebUI project everything works fine. I'm guessing there are problems when sending dynamic types accross different assemblies or something.
I did a quick test:
namespace ConsoleApplication5
{
public interface IFacebookHelper { dynamic Get(string p); }
class Program
{
static void Main(string[] args)
{
Mock<IFacebookHelper> mockFbHelp = new Mock<IFacebookHelper>();
mockFbHelp.Setup(x => x.Get("me")).Returns(new { email = "test#test.com", id = "9999" });
dynamic result = mockFbHelp.Object.Get("me");
int facebookId = int.Parse(result.id);
string email = result.email;
}
}
}
This is working fine. I don't see a problem here.
Are you sure you didn't mix some things up?
Look at the method you posted:
public dynamic Get(string p)
{
var client = new FacebookClient(AccessToken);
return client.Get("me");
}
Maybe it should be:
...
return client.Get(p);
...
Is _facebookHelper really using the Mock object? It should be of type IFacebookHelperProxy or something like that during your test.
EDIT:
The problem is your attempt to expose an anonymous type across assembly boundaries, since you can use anonymous type only within the assembly you created them.
So instead of
public class DynTestProxy:IDynTest
{
public dynamic GetData()
{
return (dynamic) new { DynamicProperty = "From Unit Test" };
}
}
you should use an ExpandoObject:
public class DynTestProxy:IDynTest
{
public dynamic GetData()
{
dynamic r = new ExpandoObject();
r.DynamicProperty = "From Unit Test";
return r;
}
}
or use the InternalsVisibleTo attribute. See here for more information. Also this question may be interesting for you.

Implicit conversion between types in C#

I have the following business objects:
public abstract class Product
{
public int Id { get; set; }
public bool OnStock { get; set; }
}
public class ProductForImport : Product
{
public int ImportId { get; set; }
}
public class ProductForExport : Product
{
public int ExportId { get; set; }
public bool IsExportable { get; set; }
public bool IsUsable { get; set; }
public string OtherParam {get; set;}
public static implicit operator ProductForExport(ProductForImport pfi)
{
ProductForExport p = new ProductForExport();
p.Id = pfi.Id;
p.IsExportable = true;
p.ExportId = 0;
return p;
}
}
so I can convert between the two types:
static void Main(string[] args)
{
ProductForExport pfe = new ProductForExport();
pfe.Id = 1;
pfe.OnStock = true;
ProductForImport pfi = new ProductForImport();
pfi.ImportId = 200;
ProductForExport pfe2 = (ProductForExport)pfi;
}
this works OK.
I have 100.000 ProductsForImport items.
If I understand correctly, if I convert them to ProductsForExport items, I'll have 100.000 +100.000 items in memory - that's reasonable.
My problem is: I have to send these "ProductForExport" objects through JSON services, each service just need some subset of the properties of each type:
servicecall1 should return ProductForExport1{ExportId,IsExportable}
servicecall2 should return ProductForExport2{ExportId,IsUsable}
Question: should I write an implicit conversion similar to the above example for these new types - ProductForExport1 and ProductForExport2 (so basically create 100.000+100.000 new objects)
or
somehow can I just "hide" the unwanted properties with some magic from the original type without the need to create new instances?
thanks,
b.
If you ned such kind of decoupling and separation of entities - you can create DTO object along with each business object and use DTO to communicate with Service.
But if you have a lot of business entities consider an other approach to avoid maintenance hell.
public sealed class ExportProductDto
{
public(ProductForExport exportProduct)
{
// initialize fields
this.ExportId = exportProduct.ExportId;
}
public int ExportId { get; private set; }
}
BTW,
An overkill solution with operator overload, use Adapter pattern to convert between product types
To decouple adapting from entities itself implement following interface your self:
public interface IProductAdapter<TImport, TExport>
{
TImport ToImportProduct(TExport exportProduct);
TExport ToExportProduct(TImport importProduct);
}
Or an other adapter approach:
// Implement this interface for ProductForImport class
// public class ProductForImport : IExportProductAdapter, Product
public interface IExportProductAdapter
{
ProductForExport ToExportProduct();
}
// Implement this interface for ProductForExport class
// public class ProductForExport : IImportProductAdapter, Product
public interface IImportProductAdapter
{
ProductForImport ToImportProduct();
}
EDIT: Answer to comments
// An example of IExportProductAdapter adapter implementation
public sealed class ProductForImport : Product, IExportProductAdapter
{
public int ImportId { get; set; }
public ProductForExport ToExportProduct()
{
ProductForExport p = new ProductForExport();
p.Id = this.Id;
p.IsExportable = true;
p.ExportId = 0;
return p;
}
}
And then instead of:
ProductForExport pfe2 = (ProductForExport)pfi;
You can do:
ProductForExport pfe2 = pfi.ToExportProduct();
I would create light objects specifically for returning through the service with only the required fields. Then use Automapper or something like that to map them.
I don't recommend using operator overloading if you can avoid it. I have seen many issues where a developer didn't realize when the operator overload was being called and something unexpected happened.
If you are using WCF, you can apply the IgnoreDataMemberAttribute to properties you wish not to serialize.
Have a look at the ScriptIgnoreAttribute to exclude properties from json serialization.
It took me a few reads but I don't think your problem is about implicit conversion as much as how to send data via json right?
If you have your object collections of Import or Export object you can use the JavaScriptSerilizer and some anonymous types to slice and dice what data you send.
You can use Linq to select specific properties of your object in a collection, and define an anonymous type "on-the-fly" to serialize out as a json string like this:
List<ProductForExport> exportList; //the list to export
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = string.Empty;
output = jss.Serialize(new
{
isExportable = True, //static named properties
iTotalProducts = exportList.Count, //dynamic values
productDataArray = exportList //all data in an array object
});
//Or build the result using just a few properties of the collection:
foreach (ExportProduct exProd in exportList)
{
output += jss.Serialize(new
{
exProd.IsExportable,
exProd.ExportID
});
}

How do I reset the result for a property in a stub without resetting the entire stub?

I'm new to Rhino Mocks, so I may be missing something completely.
Lets say I have an interface with has a half dozen properties:
public interface IFoo {
string Foo1 { get; } // Required non-null or empty
string Foo2 { get; } // Required non-null or empty
string Foo3 { get; }
string Foo4 { get; }
int Foo5 { get; }
int Foo6 { get; }
}
And an implementation which takes a similar object but without the same constraints and creates an IFoo instance:
public interface IFooLikeObject {
string FooLikeObject1 { get; } // Okay non-null or empty
string FooLikeObject2 { get; } // Okay non-null or empty
string FooLikeObject3 { get; }
string FooLikeObject4 { get; }
string FooLikeObject5 { get; } // String here instead of int
string FooLikeObject6 { get; } // String here instead of int
}
public class Foo : IFoo {
public Foo(IFooLikeObject fooLikeObject) {
if (string.IsNullOrEmpty(fooLikeObject.Foo1)) {
throw new ArgumentException("fooLikeObject.Foo1 is a required element and must not be null.")
}
if (string.IsNullOrEmpty(Foo2)) {
throw new ArgumentException("fooLikeObject.Foo2 is a required element and must not be null")
}
// Make all the assignments, including conversions from string to int...
}
}
Now in my tests I want to test both that the exceptions are thrown at the proper times and also the exceptions thrown during failed conversions from string to int.
So I need too stub out IFooLikeObject to return valid values for the values I'm not currently testing, and since I don't want to duplicate this code in every test method I extract it out into a seperate method.
public IFooLikeObject CreateBasicIFooLikeObjectStub(MockRepository mocks) {
IFooLikeObject stub = mocks.Stub<IFooLikeObject>();
// These values are required to be non-null
SetupResult.For(stub.FooLikeObject1).Return("AValidString");
SetupResult.For(stub.FooLikeObject2).Return("AValidString2");
SetupResult.For(stub.FooLikeObject5).Return("1");
SetupResult.For(stub.FooLikeObject6).Return("1");
}
This works well enough for testing Foo3 and Foo4, but when testing Foo1, 2, 5, or 6 I get:
System.InvalidOperationException : The result for IFooLikeObject.get_FooLikeObject1(); has already been setup. Properties are already stubbed with PropertyBehavior by default, no action is required
For example:
[Test]
void Constructor_FooLikeObject1IsNull_Exception() {
MocksRepository mocks = new MocksRepository();
IFooLikeObject fooLikeObjectStub = CreateBasicIFooLikeObjectStub(mocks);
// This line causes the exception since FooLikeObject1 has already been set in CreateBasicIFooLikeObjectStub()
SetupResult.For(fooLikeObjectStub.FooLikeObject1).Return(null);
mocks.ReplayAll();
Assert.Throws<ArgumentException>(delegate { new Foo(fooLikeObjectStub); });
}
How can I set it up so that I can override an individual property which already has a return value set up without having to redo all the others as well?
This can be done using the Repeat.Any() construct.
I have not tested this using the SetupResult.For Syntax, but it works with the lambda syntax:
public IFooLikeObject CreateBasicIFooLikeObjectStub(MockRepository) {
IFooLikeObject stub = MockRepository.GenerateStub<IFooLikeObject>();
// These values are required to be non-null
stub.Stub(s => s.FooLikeObject1).Return("AValidString");
stub.Stub(s => s.FooLikeObject2).Return("AValidString2");
stub.Stub(s => s.FooLikeObject5).Return("1");
stub.Stub(s => s.FooLikeObject6).Return("1");
}
[Test]
void Constructor_FooLikeObject1IsNull_Exception() {
IFooLikeObject fooLikeObjectStub = CreateBasicIFooLikeObjectStub();
// This line no longer causes an exception
stub.Stub(s => s.FooLikeObject1).Return(null).Repeat.Any(); // The Repeat.Any() is key. Otherwise the value wont be overridden.
Assert.Throws<ArgumentException>(delegate { new Foo(fooLikeObjectStub); });
}
The only caveat I've found is you can't do it twice.
I might be missing something, but have you tried just doing this?
var stub = mocks.Stub<IFooLikeObject>();
stub.FooLikeObject1 = "AValidString";
stub.FooLikeObject2 = "AValidString2";
stub.FooLikeObject5 = "1";
stub.FooLikeObject6 = "1";
With stubs, you can just set the properties to what you want them to be directly.
If the property is read only you could do it like this:
var stub = mocks.Stub<IFooLikeObject>();
stub.Stub( x => x.FooLikeObject1).Return("AValidString");
stub.Stub( x => x.FooLikeObject2).Return("AValidString2");
stub.Stub( x => x.FooLikeObject5).Return("1");
stub.Stub( x => x.FooLikeObject6).Return("1");

Categories

Resources