public void SetUp() { _client = new DropBoxClientMock_ReturnsFileContent(); _setup = new MockSetup(_client, _fileSelector); _setup.Initialize(); _viewModel = new FileContentViewModel("file.txt"); }
public void TwitterSearch_Success_Callback_Sets_Tweets() { using (var setup = new MockSetup()) { setup.Initialize(); string search = null; Action <IEnumerable <Tweet> > success = null; Action <Exception> error = null; setup .Twitter .Setup(x => x.StartAsyncSearch(It.IsAny <string>(), It.IsAny <Action <IEnumerable <Tweet> > >(), It.IsAny <Action <Exception> >())) .Callback <string, Action <IEnumerable <Tweet> >, Action <Exception> >((s, s2, e) => { search = s; success = s2; error = e; }); var viewModel = new TwitterViewModel("mySearchTerm"); var calledChanges = new List <string>(); viewModel.PropertyChanged += (s, a) => calledChanges.Add(a.PropertyName); var result = new[] { new Tweet(), new Tweet() }; success(result); Assert.AreEqual(2, calledChanges.Count); CollectionAssert.Contains(calledChanges, "Tweets"); Assert.AreEqual(2, viewModel.Tweets.Count()); CollectionAssert.Contains(viewModel.Tweets.ToList(), result[0]); CollectionAssert.Contains(viewModel.Tweets.ToList(), result[1]); } }
public void GetAllKeys() { var client = MockSetup.GetClient(); var users = client.DB().GetAllKeysAsync <User>().Result; Assert.Equal(2, users.Count()); }
public void TwitterSearch_Constructor_Requests_A_TwitterSearch() { using (var setup = new MockSetup()) { setup.Initialize(); string search = null; Action <IEnumerable <Tweet> > success = null; Action <Exception> error = null; setup .Twitter .Setup(x => x.StartAsyncSearch(It.IsAny <string>(), It.IsAny <Action <IEnumerable <Tweet> > >(), It.IsAny <Action <Exception> >())) .Callback <string, Action <IEnumerable <Tweet> >, Action <Exception> >((s, s2, e) => { search = s; success = s2; error = e; }); var viewModel = new TwitterViewModel("mySearchTerm"); setup .Twitter .Verify(x => x.StartAsyncSearch(It.IsAny <string>(), It.IsAny <Action <IEnumerable <Tweet> > >(), It.IsAny <Action <Exception> >()), Times.Once()); Assert.AreEqual("mySearchTerm", search); Assert.IsNotNull(success); Assert.IsNotNull(error); } }
public void MockSetupEqualsByInvocationAndMatchers() { var method = new Action <bool, string, PlatformID>(AMethod).Method; var args = new object[] { true, "foo", PlatformID.Win32NT }; var target = this; var any = AnyMatcher <string> .Default; Func <string, bool> condition = s => s.Length > 0; var conditional = new ConditionalMatcher <string>(condition, "foo"); var value = new ValueMatcher(typeof(string), "foo"); var setup = new MockSetup(new MethodInvocation(target, method, args), new[] { any, conditional, value }); var other = new MockSetup(new MethodInvocation(target, method, args), new[] { any, conditional, value }); Assert.True(setup.Equals(other)); Assert.Equal(setup.GetHashCode(), other.GetHashCode()); var hash = new HashSet <IMockSetup>(); Assert.True(hash.Add(setup)); Assert.False(hash.Add(other)); Assert.Contains(setup, hash); Assert.Contains(other, hash); Assert.False(setup.Equals( new MockSetup(new MethodInvocation(new object(), method, args), new[] { any, conditional, value }))); }
public void TwitterSearch_Error_Callback_Clears_IsSearching() { using (var setup = new MockSetup()) { setup.Initialize(); string search = null; Action <IEnumerable <Tweet> > success = null; Action <Exception> error = null; setup .Twitter .Setup(x => x.StartAsyncSearch(It.IsAny <string>(), It.IsAny <Action <IEnumerable <Tweet> > >(), It.IsAny <Action <Exception> >())) .Callback <string, Action <IEnumerable <Tweet> >, Action <Exception> >((s, s2, e) => { search = s; success = s2; error = e; }); var viewModel = new TwitterViewModel("mySearchTerm"); var calledChanges = new List <string>(); viewModel.PropertyChanged += (s, a) => calledChanges.Add(a.PropertyName); error(new Exception("Test exception")); Assert.AreEqual(1, calledChanges.Count); CollectionAssert.Contains(calledChanges, "IsSearching"); Assert.IsFalse(viewModel.IsSearching); Assert.IsNull(viewModel.Tweets); } }
public void TrackMockBehaviors() { var stunt = new FakeStunt(); // Forces initialization of the default mock. Assert.NotNull(stunt.Mock); var setup = new MockSetup( new MethodInvocation(stunt, typeof(FakeStunt).GetMethod("Do")), Array.Empty <IArgumentMatcher>()); var initialBehaviors = stunt.Behaviors.Count; var behavior = new MockBehaviorPipeline(setup); stunt.AddBehavior(behavior); stunt.AddBehavior(new DelegateStuntBehavior((m, n) => n().Invoke(m, n))); Assert.Equal(initialBehaviors + 2, stunt.Behaviors.Count); Assert.Single(stunt.Mock.Setups); Assert.Same(behavior, stunt.Mock.GetPipeline(setup)); stunt.Behaviors.Remove(behavior); Assert.Equal(initialBehaviors + 1, stunt.Behaviors.Count); Assert.Empty(stunt.Mock.Setups); }
public void DefaultClientBehavior() { var client = MockSetup.GetClient(); // should always be available after SetDefaultDatabase Assert.NotNull(client.DB()); }
public void AQLSelectEntity() { var client = MockSetup.GetClient(); var user = client.DB().Query <User>("for u in User return u").Result.First(); Assert.IsType <User>(user); }
public void PickRandom_Changes_SearchText() { using (var setup = new MockSetup()) { setup.Initialize(); var viewModel = new HomeViewModel(); Assert.IsNotNull(viewModel.SearchText); var calledProperties = new List <string>(); viewModel.PropertyChanged += (sender, args) => calledProperties.Add(args.PropertyName); Assert.AreEqual(0, calledProperties.Count); var existingSearchText = viewModel.SearchText; viewModel.PickRandomCommand.Execute(); Assert.AreEqual(1, calledProperties.Count); Assert.AreEqual("SearchText", calledProperties[0]); Assert.AreNotEqual(existingSearchText, viewModel.SearchText); existingSearchText = viewModel.SearchText; viewModel.PickRandomCommand.Execute(); Assert.AreEqual(2, calledProperties.Count); Assert.AreEqual("SearchText", calledProperties[0]); Assert.AreEqual("SearchText", calledProperties[1]); Assert.AreNotEqual(existingSearchText, viewModel.SearchText); } }
public void AppliesToSetup() { var invocation = new MethodInvocation(new FakeMock(), typeof(object).GetMethod(nameof(object.ToString))); var setup = new MockSetup(invocation, Array.Empty <IArgumentMatcher>()); var pipeline = new MockBehaviorPipeline(setup); Assert.True(pipeline.AppliesTo(invocation)); }
public void TwitterSearch_Constructor_Sets_IsSearching_True() { using (var setup = new MockSetup()) { setup.Initialize(); var viewModel = new TwitterViewModel("mySearchTerm"); Assert.IsTrue(viewModel.IsSearching); } }
public void CreateDatabase() { var client = MockSetup.GetClient(); MockSetup newsetup = new MockSetup(); var dbs = newsetup.settings; //dbs.DatabaseName = "newtest"; // non-existant database should be created and returned Assert.IsType <ArangoDatabase>(client.InitDB(dbs)); }
public void User_Can_Change_SearchText() { using (var setup = new MockSetup()) { setup.Initialize(); var viewModel = new HomeViewModel(); Assert.IsNotNull(viewModel.SearchText); viewModel.SearchText = "MyTestString"; Assert.AreEqual("MyTestString", viewModel.SearchText); } }
public void AddPipelineForSetupIfMissing() { var stunt = new FakeStunt(); var setup = new MockSetup( new MethodInvocation(stunt, typeof(FakeStunt).GetMethod("Do")), Array.Empty <IArgumentMatcher>()); var behavior = stunt.Mock.GetPipeline(setup); Assert.NotNull(behavior); Assert.Equal(2, stunt.Behaviors.Count); Assert.Single(stunt.Mock.Setups); }
public void UpdateAsync() { var client = MockSetup.GetClient(); var id = "1312460"; var user = new User() { Password = "******", Username = "******" }; var updated = client.DB().UpdateAsync(id, user).Result; var updatedUser = updated.New; Assert.IsType <User>(user); Assert.Equal("newpass", updatedUser.Password); }
public void CRUDMethods() { var client = MockSetup.GetClient(); var id = "1312460"; var user = new User() { Password = "******", Username = "******" }; var inserted = client.DB().InsertAsync(user).Result; var insertUser = inserted.New; Assert.IsType <User>(user); Assert.Equal("jdoe", insertUser.Username); Assert.Equal("passcode", insertUser.Password); Assert.Equal(id, inserted.Key); }
public void SearchCommand_Calls_RequestNavigate() { using (var setup = new MockSetup()) { setup.Initialize(); var requests = new List <MvxShowViewModelRequest>(); setup.Dispatcher .Setup(x => x.RequestNavigate(It.IsAny <MvxShowViewModelRequest>())) .Callback <MvxShowViewModelRequest>((request) => requests.Add(request)); var viewModel = new HomeViewModel(); viewModel.SearchText = "MyTestValue"; viewModel.SearchCommand.Execute(); setup.Dispatcher .Verify(x => x.RequestNavigate(It.IsAny <MvxShowViewModelRequest>()), Times.Once()); Assert.AreEqual(1, requests.Count); Assert.AreEqual(typeof(TwitterViewModel), requests[0].ViewModelType); Assert.AreEqual(false, requests[0].ClearTop); Assert.AreEqual(MvxRequestedBy.UserAction, requests[0].RequestedBy); Assert.AreEqual(1, requests[0].ParameterValues.Count); Assert.AreEqual("MyTestValue", requests[0].ParameterValues["searchTerm"]); } }
public MinerLifetime(MockSetup mock) { _mock = mock; }
public ParallelTransactionExecutingServiceTest(MockSetup mock) : base(new XunitAssertions()) { _mock = mock; }
/// <summary> /// 构建方法 /// </summary> /// <param name="member"></param> /// <param name="members"></param> /// <param name="exists"></param> /// <param name="typeBuilder">类型构建</param> /// <param name="sourceTypeOrInterfaceType">假如是承继父类的,则callBase就能生效</param> /// <param name="buildInterface">构建接口</param> /// <param name="found"></param> private void BuildMethod(MemberInfo member, MemberInfo[] members, List <MemberInfo> exists, TypeBuilder typeBuilder, Type sourceTypeOrInterfaceType, bool @buildInterface, MockSetup found) { var method = member as MethodInfo; if (!@buildInterface && method.IsFinal) { return; } /*泛型方法和基本方法有什么不同*/ var parameters = method.GetParameters(); var parameterTypes = new List <Type>(parameters.Length); var voidReturn = method.ReturnType == typeof(void); foreach (var parameter in parameters) { parameterTypes.Add(parameter.ParameterType); } var attributes = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.SpecialName; if (!buildInterface) { attributes = MethodAttributes.Public | MethodAttributes.ReuseSlot | MethodAttributes.Virtual | MethodAttributes.HideBySig; } var methodBuilder = typeBuilder.DefineMethod(method.Name, attributes, method.CallingConvention, method.ReturnType, parameterTypes.ToArray()); /*fix argument length*/ parameterTypes.Insert(0, sourceTypeOrInterfaceType); var il = new MockEmitBuilder(methodBuilder.GetILGenerator(), method.CallingConvention, method.ReturnType, parameterTypes.ToArray()); if (found == null) { goto _notImpl; } goto _impl; _notImpl: { if (voidReturn) { /*out 参数*/ for (var i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { il.LoadArgument((ushort)(i + 1)); var ele = parameters[i].ParameterType.GetElementType(); if (ele.IsValueType) { if (this.IsEnumType(ele) || this.IsNullableEnumType(ele)) { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(Enum.GetUnderlyingType(ele)); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(ele); } } else { il.LoadNull(); il.StoreIndirect(parameters[i].ParameterType); } } } il.Return(); return; } il.NewObject(typeof(NotImplementedException), Type.EmptyTypes); il.Throw(); il.Return(); return; }; _impl: { /*void*/ if (voidReturn) { if (found.MethodToCallType == MockSetup.MethodToCall.Exception || found.MethodToCallType == MockSetup.MethodToCall.Void) { } else { throw new ArgumentOutOfRangeException(method.Name, string.Format("the method {0}.{1} return type is void, but mock donot provide action callback;", this.TargetType.Name, method.Name)); } } else { /*返回结果类型不匹配*/ if (found.GetType() != typeof(MockSetup <,>).MakeGenericType(new[] { this.TargetType, method.ReturnType })) { goto _notImpl; } } /*返回基类类型,如果当前是接口类型的,则抛出异常*/ if (found.MethodIndex == 0) { if (@buildInterface) { goto _notImpl; } //this foreach (var m in this.GetMembers(sourceTypeOrInterfaceType)) { if (m.MemberType == MemberTypes.Method) { var pm = m as MethodInfo; var pp = pm.GetParameters(); if (pm.Name == found.Method.Name && pp.Length == found.Method.GetParameters().Length) { il.LoadArgument(0); for (ushort i = 1; i < pp.Length + 1; i++) { il.LoadArgument(i); } il.Call(pm); il.Return(); return; } } } throw new ArgumentOutOfRangeException(method.Name, string.Format("the method {0}.{1} cannot been found;", this.TargetType.Name, method.Name)); } /*用该值定位到执行的是哪个方法,因重载的原因,有15个方法以上,具体要看MockSetup<,>*/ var idx = MockSetupInfoStore.Enqueue(found); /*抛出异常*/ if (found.MethodIndex == -1) { il.LoadArgument(0); il.LoadConstant(idx); if (voidReturn) { il.Call(typeof(MockSetupInfoStore).GetMethod("Call_ExceptionWithNoResult").MakeGenericMethod(new[] { this.TargetType })); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("Call_ExceptionWithResult").MakeGenericMethod(new[] { this.TargetType, method.ReturnType })); } il.Return(); return; } var callMethodName = typeof(MockSetupInfoStore).GetMethod(string.Concat("Call_", found.MethodIndex.ToString())); var makeGenericMethodParameterTypes = new List <Type>(found.MethodIndex); makeGenericMethodParameterTypes.Add(this.TargetType); if (found.MethodIndex >= 10 && found.MethodIndex <= 25) { /*没有参数的方法*/ if (found.MethodIndex == 10) { /*out 参数*/ for (var i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { il.LoadArgument((ushort)(i + 1)); var ele = parameters[i].ParameterType.GetElementType(); if (ele.IsValueType) { if (this.IsEnumType(ele) || this.IsNullableEnumType(ele)) { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(Enum.GetUnderlyingType(ele)); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(ele); } } else { il.LoadNull(); il.StoreIndirect(parameters[i].ParameterType); } } } makeGenericMethodParameterTypes.Add(method.ReturnType); il.LoadArgument(0); il.LoadConstant(idx); il.Call(callMethodName.MakeGenericMethod(makeGenericMethodParameterTypes.ToArray())); il.Return(); return; } if (found.CallbackMethodParameters == null || found.CallbackMethodParameters.Length == 0) { throw new ArgumentOutOfRangeException(method.Name, string.Format("{0}.{1} method need {2} parameters,but mock provider 0 parameters", this.TargetType.Name, method.Name, parameterTypes.Count)); } if (parameterTypes.Count != found.CallbackMethodParameters.Length) { throw new ArgumentException(string.Format("{0}.{1} method need {2} parameters,but the mock provide {3} parameters", this.TargetType.Name, method.Name, parameters.Length.ToString(), found.CallbackMethodParameters.Length.ToString()), method.Name); } for (var i = 1; i < found.CallbackMethodParameters.Length; i++) { if (parameterTypes[i] != found.CallbackMethodParameters[i].ParameterType) { throw new ArgumentException(string.Format("{0}.{1} method the {2} parameter type is {3},but the mock provide {4} type", this.TargetType.Name, method.Name, i.ToString(), parameterTypes[i].Name, found.CallbackMethodParameters[i].ParameterType.Name), method.Name); } makeGenericMethodParameterTypes.Add(found.CallbackMethodParameters[i].ParameterType); } makeGenericMethodParameterTypes.Add(method.ReturnType); /*out 参数*/ for (var i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { il.LoadArgument((ushort)(i + 1)); var ele = parameters[i].ParameterType.GetElementType(); if (ele.IsValueType) { if (this.IsEnumType(ele) || this.IsNullableEnumType(ele)) { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(Enum.GetUnderlyingType(ele)); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(ele); } } else { il.LoadNull(); il.StoreIndirect(parameters[i].ParameterType); } } } il.LoadArgument(0); il.LoadConstant(idx); for (ushort i = 1; i < makeGenericMethodParameterTypes.Count - 1; i++) { il.LoadArgument(i); } il.Call(callMethodName.MakeGenericMethod(makeGenericMethodParameterTypes.ToArray())); il.Return(); return; } if (found.MethodIndex >= 30 && found.MethodIndex <= 45) { /*没有参数的方法*/ if (found.MethodIndex == 30) { /*out 参数*/ for (var i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { il.LoadArgument((ushort)(i + 1)); var ele = parameters[i].ParameterType.GetElementType(); if (ele.IsValueType) { if (this.IsEnumType(ele) || this.IsNullableEnumType(ele)) { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(Enum.GetUnderlyingType(ele)); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(ele); } } else { il.LoadNull(); il.StoreIndirect(parameters[i].ParameterType); } } } il.LoadArgument(0); il.LoadConstant(idx); il.Call(callMethodName.MakeGenericMethod(makeGenericMethodParameterTypes.ToArray())); il.Return(); return; } if (parameterTypes.Count != found.CallbackMethodParameters.Length) { throw new ArgumentException(string.Format("{0}.{1} method need {2} parameters,but the mock provide {3} parameters", this.TargetType.Name, method.Name, parameters.Length.ToString(), found.CallbackMethodParameters.Length.ToString()), method.Name); } for (var i = 1; i < found.CallbackMethodParameters.Length; i++) { if (parameterTypes[i] != found.CallbackMethodParameters[i].ParameterType) { throw new ArgumentException(string.Format("{0}.{1} method the {2} parameter type is {3},but the mock provide {4} type", this.TargetType.Name, method.Name, i.ToString(), parameterTypes[i].Name, found.CallbackMethodParameters[i].ParameterType.Name), method.Name); } makeGenericMethodParameterTypes.Add(found.CallbackMethodParameters[i].ParameterType); } /*out 参数*/ for (var i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { il.LoadArgument((ushort)(i + 1)); var ele = parameters[i].ParameterType.GetElementType(); if (ele.IsValueType) { if (this.IsEnumType(ele) || this.IsNullableEnumType(ele)) { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(Enum.GetUnderlyingType(ele)); } else { il.Call(typeof(MockSetupInfoStore).GetMethod("ReturnDefault").MakeGenericMethod(new[] { ele })); il.StoreIndirect(ele); } } else { il.LoadNull(); il.StoreIndirect(parameters[i].ParameterType); } } } il.LoadArgument(0); il.LoadConstant(idx); for (ushort i = 1; i < makeGenericMethodParameterTypes.Count; i++) { il.LoadArgument(i); } il.Call(callMethodName.MakeGenericMethod(makeGenericMethodParameterTypes.ToArray())); il.Return(); return; } }; }
public void DeleteAsync() { var client = MockSetup.GetClient(); Assert.True(client.DB().DeleteAsync <User>("1127162").Result); }
public void GetByKeyAsync() { var client = MockSetup.GetClient(); Assert.NotNull(client.DB().GetByKeyAsync <User>("1312460").Result); }