Mock ProtectedSessionStorage всегда получает одно и то же значениеC#

Место общения программистов C#
Ответить
Гость
 Mock ProtectedSessionStorage всегда получает одно и то же значение

Сообщение Гость »


I'm building some unit tests for Blazor using bUnit.

At the moment I'm trying to mock correctly ProtectedSessionStorage because of the usage of SessionStorage in my components.

This is the implementation of the Mock that I have right now:

public class MockDataProtectionProvider { public Mock Mock = new Mock(); private Mock? mockDataProtector; #region Private Methods #endregion public void SetAll(BunitJSInterop JSInterop) { SetCountry(JSInterop); SetLanguage(JSInterop); } public void SetLanguage(BunitJSInterop JSInterop) { Set("1", "_language", JSInterop); } public void SetCountry(BunitJSInterop JSInterop) { Set("55BBF40F-3693-478E-948A-FB2321DB7771", "_countryId", JSInterop); } private void Set(string value, string key, BunitJSInterop JSInterop) { mockDataProtector = new Mock(); // This string will be the json of the object you want // to receive when GetAsync is called from your ProtectedSessionStorage. string valueJson = JsonSerializer.Serialize(value); // Encode the valueJson directly as a byte array. byte[] valueBytes = Encoding.UTF8.GetBytes(valueJson); // Base64 used internally on the ProtectedSessionStorage. byte[] protectedValueBytes = mockDataProtector.Object.Protect(valueBytes); string base64valueJson = Convert.ToBase64String(protectedValueBytes); // Notice how the mocked methods return the json. mockDataProtector.Setup(sut => sut.Protect(It.IsAny())).Returns(protectedValueBytes); mockDataProtector.Setup(sut => sut.Unprotect(It.IsAny())).Returns(valueBytes); Mock.Setup(s => s.CreateProtector(It.IsAny())).Returns(mockDataProtector.Object); JSInterop.Setup("sessionStorage.getItem", key).SetResult(base64valueJson); } } This is the way that is being called to inject it:

//Prepare JSInterop to get set data into SessionStorage DataProtectionProviderMock.SetAll(JSInterop); _protectedSessionStorage = new ProtectedSessionStorage( JSInterop.JSRuntime, DataProtectionProviderMock.Mock.Object); Services.AddSingleton(_protectedSessionStorage); This is when I get the values injected in the component that I'm currently testing:

protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { var languageIdResult = await Storage.GetAsync(nameof(_language)); var countryIdResult = await Storage.GetAsync(nameof(_countryId)); _language = languageIdResult.Success ? languageIdResult.Value : default; _countryId = countryIdResult.Success ? countryIdResult.Value : default; } } My real problem in this implementation is that I'm getting always the same value even if I use the different keys, for example:
  • When getting the value for "_language" key, I'm getting correctly the value 1
  • When getting the value for "_countryId" key, I'm getting the value 1 that corresponds to the "_language" key

I tried different types of implementations but this is the only one that retrieves at least one of the values and in this one if I change the "Set" to set first the language instead of the country, I only get the value for the "_countryId" key, so I just assume that is overriding in some way.

The goal is to have both values correctly for each key.

Can you help me find the problem in this implementation?

Edit: forgot to mention that my .cs for the UnitTest has as base class the "TestContext" from bUnit so I can use JSInterop implemented on it


Источник: https://stackoverflow.com/questions/762 ... same-value
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»