File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed
LinkDotNet.Blog.UnitTests/Web/Shared/Services
LinkDotNet.Blog.Web/Shared/Services Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -81,13 +81,24 @@ public async Task ShouldNotStoreForAdmin()
81
81
[ Fact ]
82
82
public async Task ShouldNotThrowExceptionToOutsideWorld ( )
83
83
{
84
- localStorageService . Setup ( l => l . ContainKeyAsync ( "user" ) ) . Throws < Exception > ( ) ;
84
+ localStorageService . Setup ( l => l . SetItemAsync ( "user" , "some value ") ) . Throws < Exception > ( ) ;
85
85
86
86
Func < Task > act = ( ) => sut . StoreUserRecordAsync ( ) ;
87
87
88
88
await act . Should ( ) . NotThrowAsync < Exception > ( ) ;
89
89
}
90
90
91
+ [ Fact ]
92
+ public async Task ShouldReturnFalseWhenContainKeyOnExceptionAndCreateNewOne ( )
93
+ {
94
+ localStorageService . Setup ( l => l . ContainKeyAsync ( "user" ) ) . Throws < Exception > ( ) ;
95
+
96
+ await sut . StoreUserRecordAsync ( ) ;
97
+
98
+ repositoryMock . Verify ( l => l . StoreAsync ( It . IsAny < UserRecord > ( ) ) , Times . Once ) ;
99
+ localStorageService . Verify ( l => l . SetItemAsync ( "user" , It . IsAny < Guid > ( ) ) , Times . Once ) ;
100
+ }
101
+
91
102
[ InlineData ( "http://localhost/blogPost/12?q=3" , "blogPost/12" ) ]
92
103
[ InlineData ( "http://localhost/?q=3" , "" ) ]
93
104
[ InlineData ( "" , "" ) ]
Original file line number Diff line number Diff line change 1
1
using System ;
2
- using System . Diagnostics ;
3
2
using System . Threading . Tasks ;
4
3
using LinkDotNet . Blog . Domain ;
5
4
using LinkDotNet . Blog . Infrastructure . Persistence ;
@@ -35,7 +34,7 @@ public async Task StoreUserRecordAsync()
35
34
}
36
35
catch ( Exception e )
37
36
{
38
- Trace . Write ( $ "Exception: { e } ") ;
37
+ Console . Write ( $ "Exception: { e } ") ;
39
38
}
40
39
}
41
40
@@ -63,7 +62,7 @@ private async Task GetAndStoreUserRecordAsync()
63
62
64
63
private async Task < int > GetIdentifierHashAsync ( )
65
64
{
66
- var hasKey = await localStorageService . ContainKeyAsync ( "user" ) ;
65
+ var hasKey = await TryGetKey ( ) ;
67
66
if ( hasKey )
68
67
{
69
68
var key = await localStorageService . GetItemAsync < Guid > ( "user" ) ;
@@ -75,6 +74,20 @@ private async Task<int> GetIdentifierHashAsync()
75
74
return id . GetHashCode ( ) ;
76
75
}
77
76
77
+ private async Task < bool > TryGetKey ( )
78
+ {
79
+ try
80
+ {
81
+ var hasKey = await localStorageService . ContainKeyAsync ( "user" ) ;
82
+ return hasKey ;
83
+ }
84
+ catch ( Exception e )
85
+ {
86
+ Console . WriteLine ( $ "Couldn't obtain key: \" user\" : { e } ") ;
87
+ return false ;
88
+ }
89
+ }
90
+
78
91
private string GetClickedUrl ( )
79
92
{
80
93
var basePath = navigationManager . ToBaseRelativePath ( navigationManager . Uri ) ;
You can’t perform that action at this time.
0 commit comments