public void ConfigSettingsBase_PropertyChanges_ShouldCreateFileWithChanges() { var path = "somepath.json"; var mockFile = new Mock <IFileBase>(); var mockDirectory = new Mock <IDirectoryBase>(); var config = new ConfigSettingsBaseForTesting(path, mockFile.Object, mockDirectory.Object); config.SaveIfNotExists(); var expectedData = JsonConvert.SerializeObject(new TestData { SomeInt = DefaultSomeInt, SomeString = DefaultSomeString }); mockFile.Verify(o => o.Exists(path), Times.Exactly(2)); mockFile.Verify(o => o.WriteAllText(path, expectedData), Times.Once); config.SomeString = "other string"; config.SomeInt = 4321; var expectedData2 = JsonConvert.SerializeObject(new TestData { SomeInt = DefaultSomeInt, SomeString = "other string" }); mockFile.Verify(o => o.WriteAllText(path, expectedData2), Times.Once); Assert.AreEqual("other string", config.SomeString); var expectedData3 = JsonConvert.SerializeObject(new TestData { SomeInt = 4321, SomeString = "other string" }); mockFile.Verify(o => o.WriteAllText(path, expectedData3), Times.Once); Assert.AreEqual(4321, config.SomeInt); }
public void ConfigSettingsBase_Load_GivenNoConfigFile_ExpectDefaults() { var path = "somepath.json"; var mockFile = new Mock <IFileBase>(); mockFile.Setup(o => o.Exists(path)).Returns(false); var mockDirectory = new Mock <IDirectoryBase>(); var config = new ConfigSettingsBaseForTesting(path, mockFile.Object, mockDirectory.Object); mockFile.Verify(o => o.Exists(path), Times.Once); mockFile.Verify(o => o.ReadAllText(path), Times.Never); Assert.AreEqual(DefaultSomeInt, config.SomeInt); Assert.AreEqual(DefaultSomeString, config.SomeString); }
public void ConfigSettingsBase_SaveIfNotExists_ShouldCreateFileWithDefaults() { var path = "somepath.json"; var mockFile = new Mock <IFileBase>(); var mockDirectory = new Mock <IDirectoryBase>(); var config = new ConfigSettingsBaseForTesting(path, mockFile.Object, mockDirectory.Object); config.SaveIfNotExists(); var expectedData = JsonConvert.SerializeObject(new TestData { SomeInt = DefaultSomeInt, SomeString = DefaultSomeString }); mockFile.Verify(o => o.Exists(path), Times.Exactly(2)); mockDirectory.Verify(o => o.CreateIfNotExists(It.IsAny <string>()), Times.Once); mockFile.Verify(o => o.WriteAllText(path, expectedData), Times.Once); }
public void ConfigSettingsBase_Load_GivenInvalidConfigFile_ExpectException() { var path = "somepath.json"; var mockFile = new Mock <IFileBase>(); mockFile.Setup(o => o.Exists(path)).Returns(true); mockFile.Setup(o => o.ReadAllText(path)).Returns("some broken data"); var mockDirectory = new Mock <IDirectoryBase>(); var config = new ConfigSettingsBaseForTesting(path, mockFile.Object, mockDirectory.Object); var expectedData = JsonConvert.SerializeObject(new TestData { SomeInt = DefaultSomeInt, SomeString = DefaultSomeString }); mockFile.Verify(o => o.Exists(path), Times.Once); Assert.AreEqual(DefaultSomeInt, config.SomeInt); Assert.AreEqual(DefaultSomeString, config.SomeString); }