예제 #1
0
        public void GetStateShouldReturnInitialState()
        {
            var store = new MockStore(new MockState(k_MockStateDefault));

            Assert.That(store.GetState().Foo, Is.EqualTo(k_MockStateDefault));
            Assert.That(store.GetState().Bar, Is.EqualTo(k_MockStateDefault));
        }
예제 #2
0
        public async Task FetchTilesRequest_FakeHttpClient()
        {
            // Arrange
            var request  = new TilesState.FetchTilesRequest();
            var response = "<head><meta property=\"og:title\" content=\"Microsoft launches Spend iOS app that automatically tracks and matches expenses\" />";

            response += "<meta property=\"og:image\" content=\"image\" />";
            response += "<meta property=\"og:description\" content=\"description\" /></head>";
            var mockStore = new MockStore();

            mockStore.SetState(new TilesState());
            var handler = new FetchTilesHandler(mockStore, Pocket(), TileMakerClient(response), GetPocketIOptions());

            // Act
            var result = await handler.Handle(request, CancellationToken.None);

            // Assert
            result.Should().NotBeNull();

            var state = mockStore.GetState <TilesState>();

            state.OriginalTiles.Should().NotBeNullOrEmpty();
            var first = state.OriginalTiles.First();

            first.Title.Should().NotBeNullOrWhiteSpace();
            first.Title.Should().Be("Microsoft launches Spend iOS app that automatically tracks and matches expenses");

            TestConsole.WriteLine(first.Title);
        }
예제 #3
0
        public void GetResponse_ValidatorReturnsErrorForSomeInput_EndpointsListIsNull()
        {
            // Arrange
            var path = Data.GetPath(Data.Files.SingleEndpoint);

            var settings = new Mock <IMockSettings>();

            settings.SetupGet(m => m.Mock).Returns(new MockSettings(path));

            var validationResult = new ValidationResult();

            validationResult.AddError(new SectionName("$"), "Broken");
            _validator.Setup(m => m.Validate(It.IsAny <EndpointsRoot>(), It.IsAny <SectionName>())).Returns(validationResult);

            var store = new MockStore(settings.Object, _validator.Object, null);

            // Act
            var root = store.GetEndpointsRoot();

            // Assert
            Assert.Null(root.Endpoints);             // Even though it deserialised some values, these are not returned in the response when there is an error
            Assert.True(root.Status.IsInvalid);
            var expectedErrorMessage = $@"Configuration file was read correctly but failed validation. Errors:
  - {validationResult.Errors[0]}";

            Assert.Equal(expectedErrorMessage, root.Status.ErrorMessage);
        }
예제 #4
0
        public void ShouldDispatchAction()
        {
            var store = new MockStore(new MockState(k_MockStateDefault));

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(store.GetState().Foo, Is.EqualTo(10));
            Assert.That(store.GetState().Bar, Is.EqualTo(k_MockStateDefault));

            store.Dispatch(new ChangeFooAction(20));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(k_MockStateDefault));

            store.Dispatch(new ChangeBarAction(15));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(15));

            store.Dispatch(new ChangeBarAction(30));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(30));

            store.Dispatch(new PassThroughAction());
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(30));
        }
 public void MockedIFacesWork()
 {
     //this is obviously a pretty useless test, just shows that interfaces abstract out network calls
     MockStore store = new MockStore();
     var message = store.queue.GetMessage();
     Assert.AreEqual(null, message);
 }
예제 #6
0
        public void GetResponse_ValidatorReturnsSingleError_CreatesEndpointRootWithError()
        {
            // Arrange
            var path = Data.GetPath(Data.Files.OnlyRootJson);

            var settings = new Mock <IMockSettings>();

            settings.SetupGet(m => m.Mock).Returns(new MockSettings(path));

            var validationResult = new ValidationResult();

            validationResult.AddError(new SectionName("$"), "Broken");
            _validator.Setup(m => m.Validate(It.IsAny <EndpointsRoot>(), It.IsAny <SectionName>())).Returns(validationResult);

            var store = new MockStore(settings.Object, _validator.Object, null);

            // Act
            var root = store.GetEndpointsRoot();

            // Assert
            Assert.Null(root.Endpoints);
            Assert.True(root.Status.IsInvalid);

            var expectedErrorMessage = $@"Configuration file was read correctly but failed validation. Errors:
  - {validationResult.Errors[0]}";

            Assert.Equal(expectedErrorMessage, root.Status.ErrorMessage);
        }
예제 #7
0
        public void RegisteredObserverShouldBeCalledForEachActionDispatched()
        {
            var observer = new MockObserver();
            var store    = new MockStore(new MockState(k_MockStateDefault));

            store.Register(observer.Observe);

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(20));
            Assert.That(observer.ActionObserved, Is.EqualTo(1));

            store.Dispatch(new ChangeBarAction(10));
            Assert.That(observer.ActionObserved, Is.EqualTo(2));

            store.Dispatch(new PassThroughAction());
            Assert.That(observer.ActionObserved, Is.EqualTo(3));

            // Unregistered observer should not be notified anymore
            store.Unregister(observer.Observe);

            store.Dispatch(new PassThroughAction());
            Assert.That(observer.ActionObserved, Is.EqualTo(3));
        }
예제 #8
0
        public void RemoveValue()
        {
            IStore AppStore = new MockStore();
            string key      = "Theme";

            AppStore.RemoveValue(key);
            Assert.IsTrue(AppStore.GetValue(key) == null);
        }
예제 #9
0
        public void SetValue()
        {
            IStore AppStore = new MockStore();
            string key = "Theme", value = "Green";

            AppStore.SetValue(key, value);
            Assert.IsTrue(AppStore.GetValue("Theme") == value);
        }
예제 #10
0
    public void MockedIFacesWork()
    {
        //this is obviously a pretty useless test, just shows that interfaces abstract out network calls
        MockStore store   = new MockStore();
        var       message = store.queue.GetMessage();

        Assert.AreEqual(null, message);
    }
        public ItemViewCellModelTests()
        {
            _store = new MockStore <AppState>(AppState.SampleState);

            Guid itemId = AppState.SampleState.Index[1];

            _testItem = _store.CurrentState.Items[itemId];
            _sut      = new ItemViewCellModel(_store, itemId);
        }
예제 #12
0
        public void StoreWithBaseActionGetsNotifiedWhenUsingMoreSpecificAction()
        {
            var store = new MockStore();

            _Dispatcher.Register(store);

            _Dispatcher.Dispatch(new object());

            Assert.Equal(1, store.InvocationCount);
        }
예제 #13
0
        public void RegisteringTwiceThrows()
        {
            var observer = new MockObserver();
            var store    = new MockStore(new MockState(k_MockStateDefault));

            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            Assert.Throws(typeof(InvalidOperationException), () => store.Register <ChangeFooAction>(MockReducers.ReplaceFoo));
            store.Register(observer.Observe);
            Assert.Throws(typeof(InvalidOperationException), () => store.Register(observer.Observe));
        }
예제 #14
0
        public void StoreWithMoreSpecificActionDoesNotGetNotifiedWhenUsingBaseAction()
        {
            var store = new MockStore <string>();

            _Dispatcher.Register(store);

            _Dispatcher.Dispatch(new object());

            Assert.Equal(0, store.InvocationCount);
        }
예제 #15
0
        public void DispatchingUnregisteredActionShouldLogAnError()
        {
            var store = new MockStore(new MockState(k_MockStateDefault));

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            LogAssert.Expect(LogType.Error, $"No reducer for action type {typeof(UnregisteredAction)}");
            store.Dispatch(new UnregisteredAction());
            LogAssert.NoUnexpectedReceived();
        }
예제 #16
0
        public void RegisteringDoesNotChangeState()
        {
            var observer = new MockObserver();
            var store    = new MockStore(new MockState(k_MockStateDefault));

            store.Register(observer.Observe);

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            Assert.That(store.GetState().Foo, Is.EqualTo(k_MockStateDefault));
            store.Unregister(observer.Observe);
        }
예제 #17
0
        public void UnregisteringTwiceDoesNotThrow()
        {
            var observer = new MockObserver();
            var store    = new MockStore(new MockState(k_MockStateDefault));

            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register(observer.Observe);

            store.Unregister(observer.Observe);
            store.Unregister(observer.Observe);

            store.Unregister <ChangeFooAction>();
            store.Unregister <ChangeFooAction>();
        }
예제 #18
0
        public void DispatchedActionShouldTriggerStateChangedAfterUpdate()
        {
            int stateChangedCount = 0;
            var store             = new MockStore(new MockState(k_MockStateDefault));

            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.StateChanged += () => { stateChangedCount++; };

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(stateChangedCount, Is.EqualTo(1));

            store.Dispatch(new ChangeBarAction(20));
            Assert.That(stateChangedCount, Is.EqualTo(2));
        }
예제 #19
0
        public void GetResponse_FileIsOnlyRootJson_CreatesValidEmptyEndpointRoot()
        {
            // Arrange
            var path = Data.GetPath(Data.Files.OnlyRootJson);

            var settings = new Mock <IMockSettings>();

            settings.SetupGet(m => m.Mock).Returns(new MockSettings(path));

            var store = new MockStore(settings.Object, _validator.Object, null);

            // Act
            var root = store.GetEndpointsRoot();

            // Assert
            Assert.Null(root.Endpoints);
            Assert.False(root.Status.IsInvalid);
        }
예제 #20
0
        public void GetResponse_FileIsInvalid_CreatesInvalidEmptyEndpointRoot(string name)
        {
            // Arrange
            var path = Data.GetPath(name);

            var settings = new Mock <IMockSettings>();

            settings.SetupGet(m => m.Mock).Returns(new MockSettings(path));

            var store = new MockStore(settings.Object, _validator.Object, null);

            // Act
            var root = store.GetEndpointsRoot();

            // Assert
            Assert.Null(root.Endpoints);
            Assert.True(root.Status.IsInvalid);
            Assert.StartsWith("Error reading configuration file:", root.Status.ErrorMessage);
        }
예제 #21
0
        public void GetResponse_FileDoesNotExist_CreatesInvalidEmptyEndpointRoot()
        {
            // Arrange
            var path = Data.GetPath("/some/invalid/path/that/does/not/exist.json");

            var settings = new Mock <IMockSettings>();

            settings.SetupGet(m => m.Mock).Returns(new MockSettings(path));

            var store = new MockStore(settings.Object, _validator.Object, null);

            // Act
            var root = store.GetEndpointsRoot();

            // Assert
            Assert.Null(root.Endpoints);
            Assert.True(root.Status.IsInvalid);
            Assert.StartsWith("Error loading configuration file:", root.Status.ErrorMessage);
        }
예제 #22
0
        private void OnGetResourcesCore(object sender, ListRequestEventArgs <v11.Protocol.Discovery.GetResources, v11.Datatypes.Object.Resource> args)
        {
            var handler   = (v11.Protocol.Discovery.IDiscoveryStore)sender;
            var resources = Enumerable.Empty <v11.Datatypes.Object.Resource>();

            var uri = new EtpUri(args.Request.Body.Uri);

            if (!uri.IsEtp11)
            {
                args.FinalError = handler.ErrorInfo().InvalidUri(uri);
            }
            else if (uri.IsRootUri && Store.Dataspaces.Count > 1) // List dataspaces
            {
                resources = Store.Dataspaces
                            .Select(d => d.Resource11);
            }
            else if (uri.IsDataspaceUri) // List family versions
            {
                resources = Store.GetFamilies(EtpVersion.v11, uri)
                            .Select(f => f.Resource11);
            }
            else if (uri.IsFolderUri) // List data objects in folder
            {
                // Work around for cross-ML ETP 1.1 URIs:
                var dataType = MockStore.TryGetCorrectedDataObjectType(uri);
                resources = Store.GetObjects(EtpVersion.v11, new MockGraphContext(uri.Parent, dataType))
                            .Select(o => o.Resource11(true));
            }
            else if (uri.IsDataRootUri || uri.IsObjectUri) // List types of data objects available
            {
                resources = Store.GetSupportedTypes(EtpVersion.v11, new MockGraphContext(uri, true, false), true)
                            .Select(st => st.Resource11);
            }
            else
            {
                args.FinalError = handler.ErrorInfo().InvalidUri(uri);
            }

            args.Responses.AddRange(resources);
        }
예제 #23
0
        public async Task IncrementCounterRequest_AmountDefined()
        {
            // Arrange
            var amount  = 7;
            var request = new CounterState.IncrementCounterRequest {
                Amount = amount
            };
            var mockStore = new MockStore();

            mockStore.SetState(new CounterState());
            var handler      = new CounterState.IncrementCounterHandler(mockStore);
            var initialCount = mockStore.GetState <CounterState>().Count;

            // Act
            var result = await handler.Handle(request, CancellationToken.None);

            // Assert
            result.Should().NotBeNull();
            var state = mockStore.GetState <CounterState>();

            state.Count.Should().Be(initialCount + amount);
        }
예제 #24
0
        public async Task FetchTilesRequest_RealHttpClient()
        {
            // Arrange
            var request   = new TilesState.FetchTilesRequest();
            var mockStore = new MockStore();

            mockStore.SetState(new TilesState());
            var handler = new FetchTilesHandler(mockStore, Pocket(), RealTileMakerClient(), GetPocketIOptions());

            // Act
            var result = await handler.Handle(request, CancellationToken.None);

            // Assert
            result.Should().NotBeNull();
            var state = mockStore.GetState <TilesState>();

            state.OriginalTiles.Should().NotBeNullOrEmpty();

            foreach (var tile in state.OriginalTiles)
            {
                TestConsole.WriteLine(tile.Title);
            }
        }
예제 #25
0
        public void MultipleObserverAreSupported()
        {
            var observer1 = new MockObserver();
            var observer2 = new MockObserver();
            var store     = new MockStore(new MockState(k_MockStateDefault));

            store.Register(observer1.Observe);
            store.Register(observer2.Observe);

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(observer1.ActionObserved, Is.EqualTo(1));
            Assert.That(observer2.ActionObserved, Is.EqualTo(1));

            store.Dispatch(new PassThroughAction());
            Assert.That(observer1.ActionObserved, Is.EqualTo(2));
            Assert.That(observer2.ActionObserved, Is.EqualTo(2));

            store.Unregister(observer1.Observe);
            store.Unregister(observer2.Observe);
        }
예제 #26
0
        public void GetValue()
        {
            IStore AppStore = new MockStore();

            Assert.IsTrue(AppStore.GetValue("Theme") == "Light");
        }