private async void MainWindowViewModelAsync() { Employees = new ObservableCollection <Employee>(); _entities = new NorthwindIBEntities(); _query = _entities.Employee; IEnumerable <Employee> results = await _entities.ExecuteQueryAsync(_query); results.ForEach(Employees.Add); MoveCurrentToFirst(); _entities.EntityChanged += (sender, args) => { CanSave = ((NorthwindIBEntities)sender).HasChanges(); }; Employees.CollectionChanged += Employees_CollectionChanged; }
public async Task ShouldLoginLogout() { bool principalChangedFired = false; bool loggedInFired = false; bool loggedOutFired = false; bool managerCreatedFired = false; var auth = new AuthenticationService(); CompositionContext contextWithAuthService = CompositionContext.Fake .WithGenerator(typeof(IAuthenticationService), () => auth) .WithName("ShouldLoginLogout"); CompositionContextResolver.Add(contextWithAuthService); var connectionOptions = ConnectionOptions.Default.WithCompositionContext("ShouldLoginLogout").WithName("ShouldLoginLogout"); ConnectionOptionsResolver.Add(connectionOptions); var emp = new EntityManagerProvider <NorthwindIBEntities>() .Configure(provider => provider.WithConnectionOptions("ShouldLoginLogout")); auth.PrincipalChanged += (s, e) => principalChangedFired = true; auth.LoggedIn += (s, e) => loggedInFired = true; auth.LoggedOut += (s, e) => loggedOutFired = true; emp.ManagerCreated += (s, e) => managerCreatedFired = true; NorthwindIBEntities manager = null; await InitFakeBackingStoreAsync("ShouldLoginLogout"); await auth.LoginAsync(new LoginCredential("test", "test", null)); Assert.IsTrue(principalChangedFired, "PrincipalChanged should have been fired"); Assert.IsTrue(loggedInFired, "LoggedIn should have been fired"); Assert.IsFalse(loggedOutFired, "LoggedOut shouldn't have been fired"); Assert.IsTrue(auth.IsLoggedIn, "Should be logged in"); Assert.IsTrue(auth.Principal.Identity.Name == "test", "Username should be test"); manager = emp.Manager; Assert.IsTrue(managerCreatedFired, "ManagerCreated should have been fired"); Assert.IsNotNull(manager.AuthenticationContext.Principal, "The principal should not be null on the EntitiyManager"); Assert.IsTrue(manager.AuthenticationContext.Principal.Identity.Name == "test", "Principal should have the same username"); principalChangedFired = false; loggedInFired = false; managerCreatedFired = false; await auth.LogoutAsync(); Assert.IsTrue(principalChangedFired, "PrincipalChanged should have been fired"); Assert.IsFalse(loggedInFired, "LoggedIn shouldn't have been fired"); Assert.IsTrue(loggedOutFired, "LoggedOut should have been fired"); Assert.IsFalse(auth.IsLoggedIn, "Should be logged out"); Assert.IsNull(auth.Principal, "Principal should be null"); Assert.IsFalse(manager.IsConnected, "Old EntityManager should be disconnected"); bool exceptionThrown = false; try { await manager.Customers.ExecuteAsync(); } catch (InvalidOperationException) { exceptionThrown = true; } Assert.IsTrue(exceptionThrown, "Should have thrown an error"); manager = emp.Manager; Assert.IsTrue(managerCreatedFired, "ManagerCreated should have been fired"); Assert.IsNull(manager.AuthenticationContext.Principal, "The principal should be null on the EntitiyManager"); }