Ejemplo n.º 1
0
        public async void A_widget_is_instantiated_from_json_and_type_info_strings()
        {
            // Given a widget in the area blog sidebar1
            var widgetId = await _svc.CreateWidgetAsync(MY_WIDGET_FOLDER);

            await _svc.AddWidgetToAreaAsync(widgetId, WidgetService.BlogSidebar1.Id, 0);

            // When the meta record is retrieved
            var widgetMeta = await _metaRepo.GetAsync(widgetId);

            // I'm able to get the widget folder
            var widget = (Widget)JsonConvert.DeserializeObject(widgetMeta.Value, typeof(Widget));

            Assert.Equal(MY_WIDGET_FOLDER, widget.Folder);

            // Given a json string that represent an instance of MyWidget
            string json = @"{""age"":10,""title"":""Tags"",""id"":0, ""folder"":""MyWidget""}";
            // And the widget type I got from above
            var widgetType = await _svc.GetWidgetTypeByFolderAsync(widget.Folder);

            var type = Type.GetType(widgetType);

            // When I deserialize it
            var myWidget = (MyWidget)JsonConvert.DeserializeObject(json, type);

            // Then we get the actual instance
            Assert.Equal(10, myWidget.Age);
        }