I am having problems setting the value of a property in Rhinomocks. I need to set the initial value of the property outside the method under test and then set its value inside the method under test conditionally. Some code:
public interface IResponse
{
string ResponseText { get; set; }
}
public void ProcessResponse(IResponse response)
{
if(response.ResponseText == "Unset")
{
response.ResponseText = someService.GetResponse();//someService here is irrelvant to the question
}
}
[TestMethod]
public void ResponseValueIsSetWhenConditionIsTrueTest()
{
var mock = Mock<IResponse>.GenerateMock();
mock.Stub(x => x.ResponseText).Returns("Unset");
Processor.ProcessResponse(mock);
Assert.AreEqual("Responseval", mock.ResponseText); //Fails because the method doesn't set the value of the property.
}
I need the mock's property to have an initial value going into the Act part of the test, and allow the method under test to change that value so I can assert on it later. However mock.ResponseText is always set to "Unset", and the method never changes its value - what is going on here?
Have you tried PropertyBehavior? For example:
mock.Stub(x => x.ResponseText).PropertyBehavior();
Then in your test:
mock.ResponseText = "Unset";
Processor.ProcessResponse(mock);
Assert.AreEqual("Responseval", mock.ResponseText);
First of all, there's a difference in behavior between mocks and stubs in Rhino.Mocks. Secondly, I'm not sure what version of Rhino.Mocks you are using, but using the latest one and AAA syntax, this certainly works:
public interface IResponse
{
string ResponseText { get; set; }
}
...
[Test]
public void Test()
{
IResponse response = MockRepository.GenerateStub<IResponse>();
response.ResponseText = "value1";
Assert.AreEqual("value1", response.ResponseText);
response.ResponseText = "value2";
Assert.AreEqual("value2", response.ResponseText);
}
Related
I've recently got a small headache during unit testing one of my property setters. I wanted to setup my property to return a certain value and do not invoke setter logic because it has some heavy operations in there and I don't want that logic to affect my unit test.
I know I can move that logic to a method and then mock that new method instead but that problem made me curious and I've dug a little.
The results of my research is in FooTests class below, one of them using SetupProperty works, but makes me feel that this is not what this method is written for.
Is there a dedicated way to short-circuit setters in partial mocks in Moq?
Foo.cs:
public class Foo
{
private int _bar;
public virtual int Bar
{
get => _bar;
set
{
MagicNumber+=FooBar;
_bar = value;
}
}
private int _fooBar;
public virtual int FooBar
{
get => _fooBar;
set
{
//Complex and heavy logic that makes the magic number -value
MagicNumber = -value;
_fooBar = value;
}
}
public int MagicNumber { get; set; }
public Foo()
{
FooBar = 1;
}
}
FooTests.cs:
[TestFixture]
public class FooTests
{
//Using ordinary setup.
[TestCase(1, 2, 2, TestName = "BarSetter_BarSetToOneAndFooBarEqualsTwo_MagicNumberEqualsTwo")]
public void BarSetterTest(int bar, int fooBar, int expectedMagicNumber)
{
var fooPartialMock = new Mock<Foo> {CallBase = true};
fooPartialMock.Setup(x => x.FooBar).Returns(fooBar);
fooPartialMock.Object.Bar = bar;
Assert.AreEqual(expectedMagicNumber, fooPartialMock.Object.MagicNumber);
}
//Using callbacks.
[TestCase(1, 2, 2, TestName = "BarSetter_BarSetToOneAndFooBarEqualsTwo_MagicNumberEqualsTwo2")]
public void BarSetterTest2(int bar, int fooBar, int expectedMagicNumber)
{
var fooPartialMock = new Mock<Foo> { CallBase = true };
fooPartialMock.SetupSet(x => x.FooBar = It.IsAny<int>()).Callback<int>(x => {});
fooPartialMock.Object.Bar = bar;
Assert.AreEqual(expectedMagicNumber, fooPartialMock.Object.MagicNumber);
}
//Using SetupProperty.
[TestCase(1, 2, 2, TestName = "BarSetter_BarSetToOneAndFooBarEqualsTwo_MagicNumberEqualsTwo3")]
public void BarSetterTest3(int bar, int fooBar, int expectedMagicNumber)
{
var fooPartialMock = new Mock<Foo> { CallBase = true };
fooPartialMock.SetupProperty(x => x.FooBar);
fooPartialMock.Object.FooBar = fooBar;
fooPartialMock.Object.Bar = bar;
Assert.AreEqual(expectedMagicNumber, fooPartialMock.Object.MagicNumber);
}
}
The difference in results of the tests is caused by different behavior of Mock, that was configured. Method Setup In the first test just override getter method:
Specifies a setup on the mocked type for a call to a value returning method.
So in this case call of FooBar in constructor affects MagicNumber. Overloading of method SetupSet you used in second test is obsolete, and looks like it doesn't override the setter, it just set up an expectation, that you can verify later? on or add a callback:
Specifies a setup on the mocked type for a call to to a property setter, regardless of its value.
In this case FooBar in constructor affects the MagicNumber too. However FooBar setter is called twice: from constructor and from lambda, where it called with return value of It.IsAny<int>, which is 0. At last, SetupProperty from the third test sets up default property behavior:
Specifies that given property should have a property behavior, meaning that setting its value will cause it to be saved and later returned when the property is requested(this is also known as stubbing)
So FooBar in constructor doesn't affect MagicNumber in the third test, because the whole property is covered with stub, and you never get to FooBar setter. Therefore the third test is green. I suppose that configuration, that you implemented in third test, do what you need. You can combine it with the first one to make FooBar getter always return the same value:
fooPartialMock.SetupProperty(x => x.FooBar).Setup(x => x.FooBar).Returns(fooBar);
Hope it helps.
I want to be able to store code in a database and then execute it dynamically (using Roslyn). However, I want to be able to (inject?) properties from calling code. See below:
using Roslyn.Scripting.CSharp;
using RoslynMVCTest.Interfaces;
namespace RoslynMVCTest.Services
{
public class MyService
{
private readonly IInjectedService _injectedService;
public MyService(IInjectedService injectedService)
{
_injectedService = injectedService;
}
public bool SomeMethod()
{
string codeString = #"
using RoslynMVCTest.Interfaces;
public class SomethingDoer
{
public IInjectedService InjectedService {get;set;}
public static bool DoSomething()
{
return IInjectedService.SomeOtherMethod();
}
}";
var engine = new ScriptEngine();
var session = engine.CreateSession(_injectedService);
session.AddReference(this.GetType().Assembly);
//How do I set the property in my dynamic code to _injectedService??
var result = session.Execute<bool>("SomethingDoer.DoSomething()");
return result;
}
}
}
I realize there are probably syntax and other issues here, but it's a good representation of what I want to do. Is there a way to do this?
First I'm going to answer your question matching your original code as closely as possible. Second, I'm going to show a far more concise example that might in fact be all that you're after.
You can certainly declare your type as you've done, but a few things will have to be fixed to even get it to make sense.
Your SomethingDoer class declares a non-static InjectedService property, despite the fact that you attempt to consume that property in a static method. I will assume for the sake of discussion that you intended SomethingDoer.DoSomething to be non-static as well and will thus instanatiate that class.
public static bool DoSomething()
To:
public bool DoSomething()
The "sesion" you pass to CreateSession is your actual service. To understand why this won't work, you have to understand what the argument you pass to CreateSession means and what's done with it. What the "session" means is that all the public properties of that object are available to your scripting session as raw identifiers without the need to . reference them on any target. Thus, to get your code working, I've introduced a new class (inner to the main service class for convenience) called Session:
public class Session
{
public IInjectedService InjectedService { get; set; }
}
Furthermore, I've used this new class when invoking CreateSession:
var session = engine.CreateSession(new Session { InjectedService = _injectedService });
What this means is that the property InjectedService is now available to you within your codeString.
Perhaps most importantly, your code codeString is never actually consumed by your code! You seem to have, understandably, conceived of this process as setting up a string for your code, and then imagined that you could then invoke some arbitrary method within it. On the contrary, there is only one block of code. So if you really want to declare a whole class in your script-code, you're still going to have to consume it directly within your script-code as well. This means that the final two lines of your codeString should actually look like:
var somethingDoer = new SomethingDoer { InjectedService = InjectedService };
somethingDoer.DoSomething()";
Here we're instantiating SomethingDoer (because of change 1.) and setting the service property by the implicit InjectedService value provided by the session (because of change 2.).
For completeness, here is the fully working sample code:
namespace RoslynMVCTest.Interfaces
{
public interface IInjectedService
{
bool SomeOtherMethod();
}
}
namespace RoslynMVCTest.Services
{
using RoslynMVCTest.Interfaces;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new MyService(new InjectedService()).SomeMethod());
Console.ReadLine();
}
}
class InjectedService : IInjectedService
{
public bool SomeOtherMethod()
{
return true;
}
}
public class MyService
{
private readonly IInjectedService _injectedService;
public MyService(IInjectedService injectedService)
{
_injectedService = injectedService;
}
public class Session
{
public IInjectedService InjectedService { get; set; }
}
public bool SomeMethod()
{
string codeString = #"
using RoslynMVCTest.Interfaces;
public class SomethingDoer
{
public IInjectedService InjectedService { get; set; }
public bool DoSomething()
{
return InjectedService.SomeOtherMethod();
}
}
var somethingDoer = new SomethingDoer { InjectedService = InjectedService };
somethingDoer.DoSomething()";
var engine = new ScriptEngine();
var session = engine.CreateSession(new Session { InjectedService = _injectedService });
session.AddReference(this.GetType().Assembly);
//How do I set the property in my dynamic code to _injectedService??
var result = session.Execute<bool>(codeString);
return result;
}
}
}
Alternative Approach
If all you want to do is allow your script to run some code that interacts with your service, you can see how this is actually extremely trivial given all the points made above. Thus to concisely express what might be the intent of your original code, all you'd have to do is:
var result = session.Execute<bool>("InjectedService.SomeOtherMethod()");
The code passed in here is simply the body of the service method in the long-winded first example. Quite possibly this is all you need or want.
I have the following situation and any help would be appreciated.
public class Poco
{
public string SomeData { get;set; }
public string SomeMoreData { get;set; }
}
public class Worker
{
public Poco DoWork()
{
// do stuff
}
}
TESTING METHOD A.......
[Test]
public static void TestPocoIsPopulated()
{
var objUt = new Worker();
var actual = objUt.DoWork();
var expected = new Poco { SomeData = "reusltOne", SomeMoreData = "resultTwo" };
actual.ShouldBeEquivalentTo(expected);
}
This works fine. However, with larger tests of nested classes, using ShouldBeEquivalentTo() becomes cumbersome, and I'd like to be able to do this as follows...
EDIT: Also with Method A you cant do this....
var expected = new Poco { SomeData = [NOT_NULL] , SomeMoreData = "resultTwo" };
TESTING METHOD B.......
[Test]
public static void TestPocoIsPopulated()
{
var objUt = new Worker();
var actual = objUt.DoWork();
actual.SomeData.Should().Be("resultOne");
actual.SomeMoreData.Should().Be("resultTwo");
}
However, if I add a new property to Poco, then Testing Method B does not complain, and the property may not get tested. Using Method A however, the test will fail as ShouldBeEquivalentTo() will note that the new property is null
So, my question is, is there a method C as follows.
TESTING METHOD C.........
[Test]
public static void TestPocoIsPopulated()
{
var objUt = new Worker();
var actual = objUt.DoWork();
actual.SomeData.Should().Be("resultOne");
actual.SomeMoreData.Should().Be("resultTwo");
actual.Should().HaveAllPropertiesBeenTested().EqualTo(true); // <-------- is this possible?
}
The ultimate question to your answer is no, we don't support verifying whether all properties have been tested. That would require some kind of proxy generation technique that tracks access to a property. I must admit it is an interesting idea, so you could propose an enhancement on https://github.com/dennisdoomen/FluentAssertions/issues?state=open
I have this scenario, which I think must be pretty common:
class Parameter
{
public int someInt;
private decimal someDecimal;
public SubParameter subParameter;
}
class SubParameter
{
public string someString { get; set; }
}
I have a breakpoint at a call to a method that takes a Parameter as a parameter. I want to write a unit test where I call this method with the same exact value (a copy of the Parameter object "tree").
It is very tedious in this case to write the many lines declaring and initializing all the fields and properties of the class, which themselves might be non-primitive etc.
It would be nice if I could just right-click on the parameter variable and then have code auto-generated to create such an object.
So if at my breakpoint, my Parameter object has the value
Parameter parameter = new Parameter
{
someInt = 42,
someDecimal = 42.42m,
subParameter = new SubParameter { someString = "42" }
};
well, then that code would be generated. I could then use the generated code for my unit test.
Does such a thing exist?
Edit:
I guess I have been unclear. I know perfectly well how to write the code myself by hand.
What I want is that when I am hitting a breakpoint and watching a complex variable (or any variable for that matter), I want to be able to say: Generate code for me that creates a clone of this variable. I would use the generated code for my unit test.
Does such a tool exist?
Just create a helper method to create the parameter for you:
public void CreateParameter()
{
return new Parameter
{
someInt = 42,
someDecimal = 42.42m,
subParameter = new SubParameter { someString = "42" }
};
}
Sample use
[TestMethod]
public void MyTest()
{
SomeClass.MethodBeingTested(CreateParameter());
}
If you want to have a specific parameter value then modify the returned parameter or provide an overload which allows you to supply that value:
[TestMethod]
public void MyTest()
{
Parameter parameter = CreateParameter();
parameter.someInt = 23;
SomeClass.MethodBeingTested(parameter);
}
I usually have my CreateParameter populate the parameter with random values to reduce the possibility that the unit test happens to pass "by chance" for certain values, but will fail for others.
You can use TestInitialize for initialize test methods:
[TestClass]
public class UnitTest1
{
Parameter _parameter = null;
[TestInitialize]
public void Initialize()
{
_parameter = new Parameter
{
someInt = 42,
someDecimal = 42.42m,
subParameter = new SubParameter { someString = "42" }
};
}
[TestCleanup]
public void Cleanup()
{
_parameter = null;
}
[TestMethod]
public void MyTest1()
{
// test _parameter
}
[TestMethod]
public void MyTest2()
{
// test _parameter
}
}
How can I implement a CacheAttribute so I can have the same logic for all properties in one place?
I am doing this to cache
[TestClass]
public class All
{
public string N
{
get
{
var value =
MemoryCache.Default.Get("Website.Tests.All.N") as string;
if (value != null)
{
return value;
}
value = DateTime.Now.ToString("HH:mm:ss.ffff");
MemoryCache.Default.Add("Website.Tests.All.N", value,
new CacheItemPolicy
{
AbsoluteExpiration =
new DateTimeOffset(DateTime.Now.AddSeconds(5))
});
return value;
}
}
[TestMethod]
public void Extension()
{
var start = N;
Thread.Sleep(1000);
var end = N;
Assert.AreEqual(start, end);
}
}
And I want to use this instead
[TestClass]
public class All
{
[Cache(Duration=5000)]
public string N
{
get
{
return DateTime.Now.ToString("HH:mm:ss.ffff");
}
}
[TestMethod]
public void Extension()
{
var start = N;
Thread.Sleep(1000);
var end = N;
Assert.AreEqual(start, end);
}
}
Is there a way to make this syntax sugar?
It seems you want to "take over" the get method for the property so as to inspect a cache first before returning the "actual" value from your property. Since there is no way to perform property interception on an ad-hoc basis, you'll have to plan ahead for this facility.
One approach would be to use interfaces and write your classes to the interface. For example,
public interface IAll
{
string N { get; set; }
}
Now you can use proxies to create a wrapper around the original instance of All that also implements this interface. Furthermore, since you're fully in charge of the property now, you can check the cache whenever the getter is called. Since you'll have the PropertyInfo/MethodInfo at your disposal, you should have no trouble generating a unique key per property.
So, whenever you would instantiate an instance of All, you also instantiate this proxy, passing it the instance of All. All subsequent usages of that instance of All should instead be passed the proxy. Like any class factory implementation, this requires you to forgo use of the new operator.
Alternatively, you can use virtual methods instead of interfaces.