RhinoMocks AAA Syntax - rhino-mocks

I've spent a good part of the day trying to figure out why a simple RhinoMocks test doesn't return the value I'm setting in the return. I'm sure that I'm just missing something really simple but I can't figure it out. Here's my test:
[TestMethod]
public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
{
FileInfo fi = new FileInfo(#"c:\Myprogram.txt");
FileInfo[] myFileInfo = new FileInfo[2];
myFileInfo[0] = fi;
myFileInfo[1] = fi;
var mockSystemIO = MockRepository.GenerateMock<ISystemIO>();
mockSystemIO.Stub(x => x.GetFilesForCopy("c:")).Return(myFileInfo);
mockSystemIO.Expect(y => y.FileCopyDateCheck(#"c:\Myprogram.txt", #"c:\Myprogram.txt")).Return("Test");
CopyFiles copy = new CopyFiles(mockSystemIO);
List<string> retValue = copy.CopyvRAFiles("c:", "c:", new AdminWindowViewModel(vRASharedData));
mockSystemIO.VerifyAllExpectations();
}
I have an interface for my SystemIO class I'm passing in a mock for that to my CopyFiles class. I'm setting an expectation on my FileCopyDatCheck method and saying that it should Return("Test"). When I step through the code, it returns a null insteaed. Any ideas what I'm missing here?
Here's my CopyFiles class Method:
public List<string> CopyvRAFiles(string currentDirectoryPath, string destPath, AdminWindowViewModel adminWindowViewModel)
{
string fileCopied;
List<string> filesCopied = new List<string>();
try
{
sysIO.CreateDirectoryIfNotExist(destPath);
FileInfo[] files = sysIO.GetFilesForCopy(currentDirectoryPath);
if (files != null)
{
foreach (FileInfo file in files)
{
fileCopied = sysIO.FileCopyDateCheck(file.FullName, destPath + file.Name);
filesCopied.Add(fileCopied);
}
}
//adminWindowViewModel.CheckFilesThatRequireSystemUpdate(filesCopied);
return filesCopied;
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "vRAClientPolicy");
Console.WriteLine("{0} Exception caught.", ex);
ShowErrorMessageDialog(ex);
return null;
}
}
I would think that "fileCopied" would have the Return value set by the Expect. The GetFilesForCopy returns the two files in myFileInfo. Please Help. :)
thanks in advance!

A mock will not start returning recorded answers until it is switched to replay mode with Replay(). Stubs and mocks do no work in the same way. I have written a blog post about the difference.
Also note that you are mixing the old record-replay-verify syntax with the new arrange-act-assert syntax. With AAA, you should not use mocks and Expect. Instead, use stubs and AssertWasCalled like this:
[TestMethod]
public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
{
// arrange
FileInfo fi = new FileInfo(#"c:\Myprogram.txt");
FileInfo[] myFileInfo = new FileInfo[2];
myFileInfo[0] = fi;
myFileInfo[1] = fi;
var stubSystemIO = MockRepository.GenerateStub<ISystemIO>();
stubSystemIO.Stub(
x => x.GetFilesForCopy(Arg<string>.Is.Anything)).Return(myFileInfo);
stubSystemIO.Stub(
y => y.FileCopyDateCheck(
Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");
CopyFiles copy = new CopyFiles(mockSystemIO);
// act
List<string> retValue = copy.CopyvRAFiles(
"c:", "c:", new AdminWindowViewModel(vRASharedData));
// make assertions here about return values, state of objects, stub usage
stubSystemIO.AssertWasCalled(
y => y.FileCopyDateCheck(#"c:\Myprogram.txt", #"c:\Myprogram.txt"));
}
Note how setting up the behavior of stubs at the start is separate from the assertions at the end. Stub does not set any expectations.
The advantage of seperating behavior and assertions is that you can make less assertions per test, making it easier to diagnose why a test failed.

Does the method FileCopyDateCheck really get called with the exact strings #"c:\Myprogram.txt" and #"c:\Myprogram.txt" as arguments?
I am not sure if FileInfo is doing something with c:\. Maybe it is modified to upper case C:\, which would make your expectation not working.
Maybe try an expectation which does not check for the exact argument values
mockSystemIO.Expect(y => y.FileCopyDateCheck(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");
For more details about argument constraints see: Rhino Mocks 3.5, Argument Constraints
I am pretty sure that there are also possibilities to make the expectation case insensitive.

I think it's because your CopyvRAFiles() method isn't virtual.

Related

Type used in a using statement should be implicitly convertible to IDisposable

I have the following logic:
try
{
using (var contents = new StreamReader(file.InputStream).ReadToEnd())
{
var rows = contents.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
rows.ForEach(r => mids.Add(r.Split(',')[0]));
}
}
catch(IOException e)
{}
finally
{
contents = null;
}
In the using statement I have an error in the question. It happened probably because I use .ReadToEnd() method.
Without the using statement I would need to use try/catch/finally for a clean up (to fix veracode resource clean up issue)
How can I fix that, so I don't need to use try\catch\finally and use only the using statement?
So, using should be used with object which implements IDisposable interface. You calling ReadToEnd method which returns string and contents is not a IDisposable (because string is not).
You should use it like this:
using (var streamReader = new StreamReader(file.InputStream))
{
var contents = streamReader.ReadToEnd();
// Some actions
}
You want to clean up StreamReader, contents will be collected by GC when method will finished because it has type string.

Checking exceptions with TestCaseData parameters

I'm using NUnit 3 TestCaseData objects to feed test data to tests and Fluent Assertions library to check exceptions thrown.
Typically my TestCaseData object contains two parameters param1 and param2 used to create an instance of some object within the test and upon which I then invoke methods that should/should not throw exceptions, like this:
var subject = new Subject(param1, param2);
subject.Invoking(s => s.Add()).Should().NotThrow();
or
var subject = new Subject(param1, param2);
subject.Invoking(s => s.Add()).Should().Throw<ApplicationException>();
Is there a way to pass NotThrow() and Throw<ApplicationException>() parts as specific conditions in a third parameter in TestCaseData object to be used in the test? Basically I want to parameterize the test's expected result (it may be an exception of some type or no exception at all).
[TestCaseData] is meant for Test Case Data, not for assertions methods.
I would keep the NotThrow and Throw in separate tests to maintain readability.
If they share a lot of setup-logic, I would extract that into shared methods to reduce the size of the test method bodies.
TestCaseData accepts compile time values, whereas TestCaseSource generates them on runtime, which would be necessary to use Throw and NotThrow.
Here's a way to do it by misusing TestCaseSource.
The result is an unreadable test method, so please don't use this anywhere.
Anyway here goes:
[TestFixture]
public class ActionTests
{
private static IEnumerable<TestCaseData> ActionTestCaseData
{
get
{
yield return new TestCaseData((Action)(() => throw new Exception()), (Action<Action>)(act => act.Should().Throw<Exception>()));
yield return new TestCaseData((Action)(() => {}), (Action<Action>)(act => act.Should().NotThrow()));
}
}
[Test]
[TestCaseSource(typeof(ActionTests), nameof(ActionTestCaseData))]
public void Calculate_Success(Action act, Action<Action> assert)
{
assert(act);
}
}
I ended up using this:
using ExceptionResult = Action<System.Func<UserDetail>>;
[Test]
[TestCaseSource(typeof(UserEndpointTests), nameof(AddUserTestCases))]
public void User_Add(string creatorUsername, Role role, ExceptionResult result)
{
var endpoint = new UserEndpoint(creatorUsername);
var person = GeneratePerson();
var request = GenerateCreateUserRequest(person, role);
// Assertion comes here
result(endpoint.Invoking(e => e.Add(request)));
}
private static IEnumerable AddUserTestCases
{
get
{
yield return new TestCaseData(TestUserEmail, Role.User, new ExceptionResult(x => x.Should().Throw<ApplicationException>())
.SetName("{m} (Regular User => Regular User)")
.SetDescription("User with Regular User role cannot add any users.");
yield return new TestCaseData(TestAdminEmail, Role.Admin, new ExceptionResult(x => x.Should().NotThrow())
)
.SetName("{m} (Admin => Admin)")
.SetDescription("User with Admin role adds another user with Admin role.");
}
}
No big issues with readability, besides, SetName() and SetDescription() methods in the test case source help with that.

How could you write a salesforce test class for a simple user-agent lookup?

I'm looking to write a test for a function that just returns a value - that's it. I'm not sure how you could do that. I'm under the impression you have to use system.assert or something. New to SFDC, but have programmed in many other languages. Here's some sample code:
static String getBrowserName()
{
String userAgent = ApexPages.currentPage().getHeaders().get('User-Agent');
if (userAgent.contains('iPhone'))
return 'iPhone-Safari';
if (userAgent.contains('Salesforce'))
return 'Salesforce';
if (userAgent.contains('BlackBerry'))
return 'BlackBerry';
if (userAgent.contains('Firefox'))
return 'Firefox';
if (userAgent.contains('Safari'))
return 'Safari';
if (userAgent.contains('internet explorer'))
return 'ie';
return 'other';
}
How can you obtain 100% test coverage for that?
While Salesforce's lack of a mocking framework is infuriating because of the hoops you have to jump through when testing things like page controllers, it's important to think about what you want to test here. Assuming that what you specifically want to test is that given the user agent strings your code returns the appropriate string, then I think something like the following should work:
static String getBrowserName(string userAgentStringToTest)
{
PageReference pageRef = getPageReference(userAgentStringToTest);
String userAgent = getUserAgent(pageRef);
...
}
PageReference getPageReference(string userAgentStringToTest)
{
if(userAgentStringToTest.Length == 0)
{
return ApexPages.currentPage();
}
else
{
PageReference pageRef = new PageReference('someURL');
pageRef.getHeaders().put('User-Agent', userAgentStringToTest);
return pageRef;
}
}
String getUserAgent(PageReference pageRef)
{
pageRef.getHeaders().get('User-Agent');
}
You would then call the getBrowserName method with the empty string in your production code and with the string you want to test in your test code.
There are a few different flavours to this of course - you could overload the methods and have a parameterless method for the main code and a parameterized method for testing. It's not ideal, but I don't know of another way to do it on the force.com platform currently.
EDIT: Just for completeness, I'm adding sample tests to clarify things. My example showed how to refactor the production code to make it testable, but did not give an example of how to write a test like the OP asked for.
Your tests would look something like this:
static testMethod void checkIPhoneBrowser()
{
String actualBrowserName = getBrowserName('string containing iPhone somewhere');
String expectedBrowserName = 'iPhone-Safari';
System.assertEquals(expectedBrowserName , actualBrowserName );
}
static testMethod void checkIEBrowser()
{
String actualBrowserName = getBrowserName('string containing internet explorer somewhere');
String expectedBrowserName = 'ie';
System.assertEquals(expectedBrowserName , actualBrowserName );
}
...

Testing multiple db mappings in MappingIntegrationTests

What's the best approach for testing multiple db's in a s#arparch project?
The current SetUp() code in MappingIntegrationTests tries to test each assembly against the first database.
string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
"../../../../app/Humanities.IBusiness.Web/NHibernate.config");
Has anyone managed to correctly test each mapping against the appropriate database schema?
Hey Alec, thanks for the reply. I've hacked a bit of a solution - it aint pretty but it does smoke test dodgy mappings across multiple db's
In the set up I add the following:
private List<string> sessionKeys;
[SetUp]
public virtual void SetUp()
{
string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
"../../../../app/Humanities.IBusiness.Web/NHibernate.config");
/*NEW CODE */
var configuration2 = NHibernateSession.AddConfiguration(DataGlobals.ROLES_DB_FACTORY_KEY,
mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
"../../../../app/Humanities.IBusiness.Web/NHibernateForRolesDb.config",null,null, null);
sessionKeys = new List<string>();
sessionKeys.Add(DataGlobals.DEFAULT_DB_KEY);
sessionKeys.Add(DataGlobals.ROLES_DB_FACTORY_KEY);
Then in the CanConfirmDatabaseMatchesMappings
foreach (var entry in allClassMetadata)
{
bool found = false;
foreach (string key in sessionKeys)
{
ISession session = NHibernateSession.CurrentFor(key);
try
{
session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
.SetMaxResults(0).List();
found = true;
}
catch (Exception ex) { }
}
if (found == false)
throw new MappingException("Mapping not found for " + entry.Key.ToString());
}
Not sure if it's a full answer but better than nothing :)
Any thoughts?
Al,
to be honest I do not know the extent that the users are using Multiple Databases. This is something I believe a few very vocal people lobbied for in the beginning of the projects lifecycle. This is something I was going to ask the community about, looks like the time has come for the question to be asked.
What you might need to do is move the set-up code into individual methods. While I am not crazy with breaking the DRY principal, it would seem in this case it is required.
Alec

Rhino AutoMocker and Stubs

I am using Joshua Flanagan article “Auto mocking Explained” as a guide. In the article there is a section called “The same tests, with automocker would look like this”. I used this information to build code to run the automocker.
As you can see below answer is a list returned from the BLL. Answer does have one row in it; however, all fields are null. So the test for boo fails. Any tips and hints would be greatly appreciated.
[Test]
public void GetStaffListAndRolesByTeam_CallBLLWithDALStub()
{
// Build List<> data for stub
List<StaffRoleByTeamCV> stubData = new List<StaffRoleByTeamCV>();
StaffRoleByTeamCV stubRow = new StaffRoleByTeamCV();
stubRow.Role = "boo";
stubRow.StaffId = 12;
stubRow.StaffName = "Way Cool";
stubData.Add(stubRow);
// create the automocker
var autoMocker = new RhinoAutoMocker<PeteTestBLL>();
// get instance of test class (the BLL)
var peteTestBllHdl = autoMocker.ClassUnderTest;
// stub out call to DAL inside of BLL
autoMocker.Get<IPeteTestDAL>().Stub(c => c.GetStaffListAndRolesByTeam("4146")).Return(stubData);
// make call to BLL this should return stubData
List<StaffRoleByTeamCV> answer = peteTestBllHdl.GetStaffListAndRolesByTeam("4146");
// do simple asserts to test stubData present
// this passes
Assert.IsTrue(1 == answer.Count, "Did not find any rows");
// this fails
Assert.IsTrue(answer[0].Role == "boo", "boo was not found");
}
I tried using MockMode.AAA but still no joy
An new version of AutoMocker (1.0.3) is available. The new version supports relay mode as in this example..
[TestMethod]
public void ShouldSupportOrderedTest()
{
//Arrange
var autoMocker = new RhinoAutoMocker<CustomerUpdater>();
var mockRepository = autoMocker.Repository;
using (mockRepository.Ordered())
{
autoMocker.Get<ICustomerDataProvider>().Expect(x => x.GetCustomer(Arg<int>.Is.Anything)).Return(new CustomerItem());
autoMocker.Get<ICustomerDataProvider>().Expect(x => x.UpdateCustomer(Arg<CustomerItem>.Is.Anything));
autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
autoMocker.Get<IMailSender>().Expect(x => x.SendMail(Arg<string>.Is.Anything, Arg<string>.Is.Anything));
}
//Act
autoMocker.ClassUnderTest.UpdateCustomerName(1, "Frank", "frank#somecompany.com");
//Assert
ExceptionAssert.Throws<ExpectationViolationException>(mockRepository.VerifyAll,"IMailSender.SendMail(anything, anything); Expected #1, Actual #0.\r\nILogWriter.Write(anything); Expected #1, Actual #0.\r\n");
}
I haven't tried, but this article suggests that by default all the mocks created by automocker are not replayed:
http://www.lostechies.com/blogs/joshuaflanagan/archive/2008/09/25/arrange-act-assert-with-structuremap-rhinoautomocker.aspx
Yes that was true for the previous version. But was changed to support ordered tests in version 1.0.3.