Added test to check Config behavior.

This commit is contained in:
Taloth Saldono 2015-01-22 19:46:55 +01:00
parent 3a938e18fa
commit 6e179839d9
3 changed files with 46 additions and 4 deletions

View File

@ -1,6 +1,9 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Authentication;
@ -13,6 +16,8 @@ namespace NzbDrone.Common.Test
public class ConfigFileProviderTest : TestBase<ConfigFileProvider>
{
private string _configFileContents;
[SetUp]
public void SetUp()
{
@ -20,8 +25,24 @@ namespace NzbDrone.Common.Test
var configFile = Mocker.Resolve<IAppFolderInfo>().GetConfigPath();
if (File.Exists(configFile))
File.Delete(configFile);
_configFileContents = null;
WithMockConfigFile(configFile);
}
protected void WithMockConfigFile(string configFile)
{
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(configFile))
.Returns<string>(p => _configFileContents != null);
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.ReadAllText(configFile))
.Returns<string>(p => _configFileContents);
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.WriteAllText(configFile, It.IsAny<string>()))
.Callback<string, string>((p, t) => _configFileContents = t);
}
[Test]
@ -142,8 +163,28 @@ namespace NzbDrone.Common.Test
Subject.SaveConfigDictionary(dic);
Subject.Port.Should().Be(port);
}
[Test]
public void SaveDictionary_should_only_save_specified_values()
{
int port = 20555;
int origSslPort = 20551;
int sslPort = 20552;
var dic = Subject.GetConfigDictionary();
dic["Port"] = port;
dic["SslPort"] = origSslPort;
Subject.SaveConfigDictionary(dic);
dic = new Dictionary<string, object>();
dic["SslPort"] = sslPort;
Subject.SaveConfigDictionary(dic);
Subject.Port.Should().Be(port);
Subject.SslPort.Should().Be(sslPort);
}
}

View File

@ -339,7 +339,7 @@ namespace NzbDrone.Core.Configuration
{
if (_diskProvider.FileExists(_configFile))
{
return XDocument.Load(_configFile);
return XDocument.Parse(_diskProvider.ReadAllText(_configFile));
}
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

View File

@ -6,6 +6,7 @@ using Moq;
using NLog;
using NUnit.Framework;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Messaging.Events;