When i run my tests im having this error, tests are working correctly but i didnt understand why this error happening
Here is tests
using iCare.Core;
using NUnit.Framework;
using Sisus.Init;
using UnityEngine;
namespace iCare.Utilities.Tests.EditMode
{
internal sealed class MockItem
{ }
internal sealed class MockStorage : Storage<MockItem>
{ }
internal sealed class StorageTests
{
MockStorage _storage;
StorageData _storageData;
[SetUp]
public void SetUp()
{
var mockLogger = ScriptableObject.CreateInstance<InformerAsset>();
_storageData = new StorageData();
_storage = new GameObject().AddComponent<MockStorage, StorageData, InformerAsset>(_storageData,
mockLogger);
}
[Test]
public void Add_AddsItemToStorage()
{
var item = new MockItem();
_storage.Add(item);
Assert.Contains(item, _storage.Items);
}
[Test]
public void Remove_RemovesItemFromStorage()
{
var item = new MockItem();
_storage.Add(item);
_storage.Remove(item);
Assert.IsFalse(_storage.Items.Contains(item));
}
[Test]
public void TryAdd_AddsItemToStorageIfCanAdd()
{
var item = new MockItem();
Assert.IsTrue(_storage.TryAdd(item));
Assert.Contains(item, _storage.Items);
}
[Test]
public void TryAdd_DoesNotAddItemToStorageIfCannotAdd()
{
var item = new MockItem();
_storageData.SetMaxCapacity(1);
_storage.Add(new MockItem());
Assert.IsFalse(_storage.TryAdd(item));
Assert.IsTrue(_storage.Items.Count == 1);
}
[Test]
public void TryRemove_RemovesItemFromStorageIfCanRemove()
{
var item = new MockItem();
_storage.Add(item);
Assert.IsTrue(_storage.TryRemove(item));
Assert.IsFalse(_storage.Items.Contains(item));
}
[Test]
public void TryRemove_DoesNotRemoveItemFromStorageIfCannotRemove()
{
var item = new MockItem();
Assert.IsFalse(_storage.TryRemove(item));
}
[Test]
public void SwapTo_RemovesItemFromThisStorageAndAddsToOtherStorage()
{
var item = new MockItem();
var otherStorage = new GameObject().AddComponent<MockStorage, StorageData, InformerAsset>(_storageData,
ScriptableObject.CreateInstance<InformerAsset>());
_storage.Add(item);
_storage.SwapTo(otherStorage, item);
Assert.IsFalse(_storage.Items.Contains(item));
Assert.Contains(item, otherStorage.Items);
}
[Test]
public void TrySwapTo_RemovesItemFromThisStorageAndAddsToOtherStorageIfCanSwap()
{
var item = new MockItem();
var otherStorage = new GameObject().AddComponent<MockStorage, StorageData, InformerAsset>(_storageData,
ScriptableObject.CreateInstance<InformerAsset>());
_storage.Add(item);
Assert.IsTrue(_storage.TrySwapTo(otherStorage, item));
Assert.IsFalse(_storage.Items.Contains(item));
Assert.Contains(item, otherStorage.Items);
}
[Test]
public void TrySwapTo_DoesNotRemoveItemFromThisStorageAndDoesNotAddToOtherStorageIfCannotSwap()
{
var item = new MockItem();
var otherStorage = new GameObject().AddComponent<MockStorage, StorageData, InformerAsset>(_storageData,
ScriptableObject.CreateInstance<InformerAsset>());
Assert.IsFalse(_storage.TrySwapTo(otherStorage, item));
Assert.IsFalse(_storage.Items.Contains(item));
Assert.IsFalse(otherStorage.Items.Contains(item));
}
}
}
Here is original code
using System.Collections.Generic;
using iCare.Core;
using Sisus.Init;
using UnityEngine;
namespace iCare.Utilities
{
[System.Serializable]
public sealed class StorageData
{
[SerializeField] int maxCapacity = 10;
internal int MaxCapacity => maxCapacity;
public void SetMaxCapacity(int value)
{
maxCapacity = value;
}
}
public abstract class Storage<T> : MonoBehaviour<StorageData, InformerAsset>
{
StorageData _storageData;
InformerAsset _informer;
public readonly List<T> Items = new();
protected override void Init(StorageData storageData, InformerAsset informerAsset)
{
_storageData = storageData;
_informer = informerAsset;
}
public void Add(T item)
{
Items.Add(item);
_informer.Info($"Added {item.Highlight()} to storage: " + name.Highlight(), this);
}
public void Remove(T item)
{
Items.Remove(item);
_informer.Info($"Removed {item.Highlight()} from storage: " + name.Highlight(), this);
}
public void SwapTo(Storage<T> otherStorage, T item)
{
Remove(item);
otherStorage.Add(item);
_informer.Info(
$"Swapped {item.Highlight()} from storage: " + name.Highlight() + " to storage: " +
otherStorage.name.Highlight(), this);
}
public bool TrySwapTo(Storage<T> otherStorage, T item)
{
if (!CanRemove(item) || !otherStorage.CanAdd(item)) return false;
SwapTo(otherStorage, item);
return true;
}
public bool TryAdd(T item)
{
if (!CanAdd(item)) return false;
Add(item);
return true;
}
public bool TryRemove(T item)
{
if (!CanRemove(item)) return false;
Remove(item);
return true;
}
public virtual bool CanAdd(T item)
{
return Items.Count < _storageData.MaxCapacity;
}
public virtual bool CanRemove(T item)
{
return Items.Contains(item);
}
}
}