MVC shop project for software architecture, in Unity.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

102 lines
5.0 KiB

using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace Tests
{
public class ShopUnitTests
{
private ShopView shopView; //This is the grid buy view we want to test
//Setup the test scene
[OneTimeSetUp]
public void LoadShopScene()
{
// Load the Scene to do unit test. In the scope of this project, this is fine. In a more complicated project, a game scene could take
// a long time to load, in which case it's better to create test scenes to do unit tests
SceneManager.LoadScene(0);
}
//Setup the unit tests here
[UnitySetUp]
public IEnumerator SetupTests()
{
yield return
null; //yield return null skips one frame, this is to make sure that this happens after the scene is loaded
//The shop scene only contains one grid buy view, we use Resources.FindObjectsOfTypeAll to get the reference to it,
//Resources.FFindObjectsOfTypeAll is used instead of GameObject.Find because the later can't find disabled objects
shopView = Resources.FindObjectsOfTypeAll<ShopViewGrid>()[0];
//Active the gridBuyView game object to initialize the class, if we don't do this 'void Start()' won't be called
//You should active all the game objects that are involved in the test before testing the functions from their components
shopView.gameObject.SetActive(true);
}
// Use meaningful name for your test cases, this case tests if the ShopGridBuyView component has initialized its ShopModel property
[UnityTest]
public IEnumerator ShopGridBuyViewInitializedShopModel()
{
yield return null; //yield return null skips one frame, waits for the Unity scene to load
//now test if a ShopModel is assigned to gridBuyView
Assert.IsNotNull(shopView.ShopModel, "No Model is assigned in ShopView");
}
//This case tests if the grid buy view displays the correct amount of Items
[UnityTest]
public IEnumerator ShopGridBuyViewDisplaysCorrectAmountOfItems()
{
yield return null; //yield return null skips one frame, waits for the Unity scene to load
//Now that the scene is loaded and the gridBuyView game object was activated in SetupTests(), we can use GameObject.Find
//to find the game object we want to test
GameObject gridItemsPanel = GameObject.Find("GridItemsPanel");
yield return
new WaitForEndOfFrame(); //Since we are testing how many items are displayed, we should use WaitForEndOfFrame to wait until the end of the frame,
//so that the view finished updating and rendering everything
int itemCount = gridItemsPanel.transform.childCount;
Assert.AreEqual(shopView.ShopModel.inventory.GetItemCount(), itemCount,
"The generated item count is not equal to shopModel's itemCount");
}
//This case tests if the buyModel can throw an ArgumentOutOfRangeException when it's asked to select an item by a negative
//index. Incorrect indexes can be generated from bugs in views or controllers, throwing the correct type of exceptions is
//better than failing silently for debugging. Your unit tests should cover exception handlings
[UnityTest]
public IEnumerator ShopModelThrowsExceptionsWhenSelectingNegativeIndex()
{
//yield return null skips one frame, waits for the Unity scene to load and buyModel to be assigned
yield return null;
//Creates a delegate that call gridBuyView.ShopModel.SelectItemByIndex(-1), the test runner will run the function, and
//check if an ArgumentOutOfRangeException is thrown, the unit test would fail if no ArgumentOutOfRangeException
//was thrown
Assert.Throws<System.ArgumentOutOfRangeException>(delegate
{
shopView.ShopModel.SelectItemByIndex(-1);
});
}
//This case tests whether info panels and selection highlights work correctly when a specific item is selected, while disabling correctly when it is unselected
[UnityTest]
public IEnumerator UpdateViewItemInfoPanelAndSelection()
{
yield return null;
Item item = shopView.ShopModel.GetSelectedItem();
//Creates a delegate that call gridBuyView.ShopModel.SelectItemByIndex(-1), the test runner will run the function, and
//check if an ArgumentOutOfRangeException is thrown, the unit test would fail if no ArgumentOutOfRangeException
//was thrown
Assert.Throws<System.Exception>(delegate
{
shopView.OnSelected(item);
},"Error selecting specified item");
Assert.IsFalse(true,"Previous item is still selected!");
}
}
}