public
bool MyHelperMethod(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)
Is there a way to use the "Returns" method to return a value that depends on the argument values passed in, rather than returning a hardcoded value?
I am thinking of something along the lines of:
Mock.Arrange(() => helper.MyHelperMethod(Arg.AnyInt, out set1, out set2, out set3, out set4))
.DoInstead(() =>
{
// mock implementation omitted
})
.Returns(
(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)
=> /* some return value computed from the arguments */);
If this is not possible (due to the "out" parameters in the method signature), can I do what I want to do by writing multiple Arrange statements, each matching on different parameter values, and then return a different value in each of these Arrange statements?
Thanks,
Asim
If you are unit testing a method that gets an Arranged mocked property and, then, sets that property, how do you verify the results at then end?
For example, let's say we want to unit test a method that looks like:
public void MyMethod() { if (_view.SelectedObject != null) _view.SelectedObject = null; } I would expect to be able to write a test similar to:
[TestMethod] public void MyMethodTest() { var view = Mock.Create<iView>(); var testObject = new Thingy(); Mock.Arrange(() => view.SelectedObject).Returns(testObject); Mock.ArrangeSet(() => view.SelectedObject = null); objectUnderTest.MyMethod(); Assert.IsNull(view.SelectedObject); } The assert fails because the mocked property return is still in effect and you get testObject. What's the proper way to write this test? Thanks!
protected override MppProfile GetAuthenticatedProfile(){ if (this.LoadAuthenticated && _authenticatedProfile == null) { try { if (this.Context.User != null) { try { foreach (MembershipProvider mp in Membership.Providers) { MembershipUser user = mp.GetUser(Context.User.Identity.Name, false); if (user != null) { _authenticatedProfile = AuthDBContext.MppProfiles.SingleOrDefault(x => x.AspnetUser.UserName == user.UserName); break; } } } catch (ConfigurationErrorsException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); throw new Exception(ex.BareMessage); } } } catch (ArgumentException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); // No authenticated user } catch (SqlException ex) { throw ex; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); throw new InvalidOperationException("Membership must be configured to load the authenticated user. Either configure a membership provider or set LoadAuthenticated=false."); } } return _authenticatedProfile;}sample function//Try to Produce Error of more than 9 Parameters for JustMockvoid GetTestFunc(int param1, int param2, int param3, int param4, int param5,
string sParam6, string sParam7, string sParam8, string sParam9, string sParam10)
{string _nothing= sParam10;}//Inside Test Project Mock.Arrange(()=> GetTestFunc(Arg.AnyInt, Arg.AnyInt,Arg.AnyInt, Arg.AnyInt,Arg.AnyInt, Arg.AnyString,Arg.AnyString,Arg.AnyString,Arg.AnyString,Arg.AnyString)) .DoInstead((int one, int two, int three, int four, int five, string a, string b, string c, string d, string e) => { string _a = e; });public interface IProcessDataPersister{List<TaskWarning> GetTaskWarnings(Guid taskId);}//in a methodvar localPersister = Mock.Create<IProcessDataPersister>();Mock.Arrange(() => localPersister.GetTaskWarnings(new Guid("{00000000-0000-0000-0001-000000000003}"))).Returns(new List<TaskWarning>() { new TaskWarning(new Guid("{00000000-0000-0000-0001-000000000003}")) { EscalationLevel = 0 } }).MustBeCalled();public static MppProfile GetUser(this IQueryable<MppProfile> profiles, string userName) { return profiles.SingleOrDefault(x => x.AspnetUser.UserName.Equals(userName));}Assert.IsNotNull(_authDBContext.MppProfiles.GetUser("steve@medportal.ca"), "Failed on lowercase test MppProfiles");_authDBContext.MppProfiles.GetUser(), but if I did that isn't that just testing JustMock itself as what I really want to test is if profiles.SingleOrDefault isn't throwing an error? Or am I looking at this the wrong way?