Skip to content

Commit 4810803

Browse files
committed
Little bit of refactoring
Just trying to keep that testability
1 parent ed9ebcc commit 4810803

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

Jump.Location.Specs/DirectoryWaitPeriodSpec.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Moq;
23
using Xunit;
34
using Should;
45

@@ -10,18 +11,17 @@ public class DirectoryWaitPeriodSpec
1011
public void It_updates_the_records_weight_upon_CloseAndUpdate()
1112
{
1213
var now = DateTime.Now.AddMinutes(-3);
13-
var record = new Record("x::y", 0);
14-
var waitPeriod = new DirectoryWaitPeriod(record, now);
14+
var recordMock = new Mock<IRecord>();
15+
var waitPeriod = new DirectoryWaitPeriod(recordMock.Object, now);
1516

1617
waitPeriod.CloseAndUpdate();
17-
18-
Assert.True(record.Weight > 0);
18+
recordMock.Verify(x => x.AddTimeSpent(It.IsAny<TimeSpan>()));
1919
}
2020

2121
[Fact]
2222
public void You_cant_call_CloseAndUpdate_twice()
2323
{
24-
var waitPeriod = new DirectoryWaitPeriod(new Record("x::y", 0), DateTime.Now);
24+
var waitPeriod = new DirectoryWaitPeriod(Mock.Of<IRecord>(), DateTime.Now);
2525
waitPeriod.CloseAndUpdate();
2626
Assert.Throws<InvalidOperationException>(() => waitPeriod.CloseAndUpdate());
2727
}

Jump.Location.Specs/Jump.Location.Specs.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="Moq">
36+
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
37+
</Reference>
3538
<Reference Include="Should">
3639
<HintPath>..\packages\Should.1.1.12.0\lib\Should.dll</HintPath>
3740
</Reference>

Jump.Location.Specs/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Moq" version="4.0.10827" />
34
<package id="Should" version="1.1.12.0" />
45
<package id="xunit" version="1.9.1" />
56
<package id="xunit.extensions" version="1.9.1" />

Jump.Location/DirectoryWaitPeriod.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace Jump.Location
44
{
55
class DirectoryWaitPeriod
66
{
7-
private readonly Record record;
7+
private readonly IRecord record;
88
private readonly DateTime startTime;
99
private bool hasDestroyed;
1010

11-
public DirectoryWaitPeriod(Record record, DateTime now)
11+
public DirectoryWaitPeriod(IRecord record, DateTime now)
1212
{
1313
this.record = record;
1414
startTime = now;
@@ -18,9 +18,7 @@ public void CloseAndUpdate()
1818
{
1919
if (hasDestroyed) throw new InvalidOperationException("You can't call CloseAndUpdate multiple times");
2020

21-
var seconds = (DateTime.Now - startTime).TotalSeconds;
22-
record.Weight += (decimal) seconds;
23-
21+
record.AddTimeSpent(DateTime.Now - startTime);
2422
hasDestroyed = true;
2523
}
2624
}

Jump.Location/Record.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
namespace Jump.Location
44
{
5-
class Record
5+
public interface IRecord
6+
{
7+
string Provider { get; }
8+
string Path { get; }
9+
string FullName { get; set; }
10+
decimal Weight { get; }
11+
void AddTimeSpent(TimeSpan timeSpan);
12+
}
13+
14+
class Record : IRecord
615
{
716
public Record(string fullName, decimal weight)
817
{
@@ -28,6 +37,11 @@ public string FullName
2837
}
2938
}
3039

31-
public decimal Weight { get; set; }
40+
public decimal Weight { get; private set; }
41+
42+
public void AddTimeSpent(TimeSpan timeSpan)
43+
{
44+
Weight += (decimal) timeSpan.TotalSeconds;
45+
}
3246
}
3347
}

0 commit comments

Comments
 (0)