have numerous tests working, but my actual test of some objects is failing and I am not sure why. When I step through the code in a simple test, I can see that what I am trying to test is defined, but PHPUnit fails the testing.
I have small code samples edited below to try to illustrate the issue without too much noise.
Very simple subset of a class definition.
class UTIL_CATEGORY_SCOPE extends UTIL_MESSAGE_DATA
{
function __construct($CategoryNo = NULL, $CategoryName = NULL)
{
$this->DeclareClassFields_();
$this->CategoryName = $CategoryName;
$this->CategoryNo = $CategoryNo;
}
private function DeclareClassFields_()
{
$this->Fields['CategoryNo'] = new UTIL_ICAP_FIELD('CCL', 6, 'Category', 8);
$this->Fields['SubCategoryNo'] = new UTIL_ICAP_FIELD('SCC', 6, 'Sub-Category', 12);
$this->Fields['SubSubCategoryNo'] = new UTIL_ICAP_FIELD('SSC', 6, 'Sub-Sub-Category', 12);
}
}
Normal code in Netbeans debugger to see this works.
$Category = new UTIL_CATEGORY_SCOPE();
Dump contents for comparison for the PHPUnit test samples to follow.
var_dump($Category);
class UTIL_CATEGORY_SCOPE#1 (4) {
public $TheMessage_ =>
class MESSAGE_DATA#2 (0) {
}
This is OK. Expected and works, as the class InstanceOf can be tested and passes correctly.
Internally, there is a list of fields maintained in an array, which is an array of class objects, that uses magic methods for get/set values, etc... Is the Array initialized?
var_dump($Category->Fields);
array(3) {
'CategoryNo' =>
class UTIL_ICAP_FIELD#3 (14) {
public $FieldCode =>
string(3) "CCL"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(8) "Category"
}
'SubCategoryNo' =>
class UTIL_ICAP_FIELD#4 (14) {
public $FieldCode =>
string(3) "SCC"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(12) "Sub-Category"
}
'SubSubCategoryNo' =>
class UTIL_ICAP_FIELD#5 (14) {
public $FieldCode =>
string(3) "SSC"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(16) "Sub-Sub-Category"
}
}
Array is initialized as expected.
Check that the Array exists and the individual items may be accessed.
if (array_key_exists('CategoryNo', $Category->Fields))
echo 'Array Key Exists';
Array Key Exists
if( $Category->Fields['CategoryNo'] instanceof UTIL_ICAP_FIELD )
echo 'Yes';
Yes
All is reporting as expected.
PHPUnit Test partial:
class TEST_UTIL_CATEGORY_SCOPE extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testObjectCreation()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertInstanceOf('UTIL_CATEGORY_SCOPE', $CategoryInfo);
$this->assertInstanceOf('UTIL_DATA_STRUCTURE', $CategoryInfo);
}
public function testConstructFieldOrder()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE(1500, 'Category Name');
$this->assertEquals(1500, $CategoryInfo->CategoryNo);
$this->assertEquals('Category Name', $CategoryInfo->CategoryName);
}
/**
*
*/
public function testConstructDefaults()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertNull($CategoryInfo->CategoryNo);
$this->assertNull($CategoryInfo->CategoryName);
}
These tests pass as expected. However, given the dumps of information above, the following 2 tests fail, even though the test run and var_dump show that the values are initialized and present as expected.
public function testFieldsCreated()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertArrayHasKey('CategoryNo', $CategoryInfo->Fields);
$this->assertArrayHasKey('SubCategoryNo', $CategoryInfo->Fields);
$this->assertArrayHasKey('SubSubCategoryNo', $CategoryInfo->Fields);
}
Errors Created:
1) TEST_UTIL_CATEGORY_SCOPE::testFieldsCreated
Failed asserting that an array has the key 'CategoryNo'.
Next Test
public function testICAPFieldTypes()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['CategoryNo']);
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['SubCategoryNo']);
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['SubSubCategoryNo']);
}
Errors Created:
2) TEST_UTIL_CATEGORY_SCOPE::testICAPFieldTypes
Failed asserting that null is an instance of class "UTIL_ICAP_FIELD".
I am not sure how to continue as this causes errors when I can see that the objects are created, the arrays populated as I would expect. I have to mark these tests as Incomplete for now, to allow our test suite to continue.
Does anyone have any ideas?
I found the solution, after some deep inspection of the class. The array I was attempting to access was defined as PROTECTED, so the PHPUnit Test Framework could not access the value.
There is two solutions to get around this.
Declare the array as PUBLIC to make it accessible.
Use Reflection in the Tests to make it accessible for the tests only.
Option 2 is our preferred solution, but for the immediate needs of the project, and the time for the other changes, Option 1 (PUBLIC) was used temporarily.
Thanks to those who helped to try to solve my issue.
Related
I am very new to unit tests and recently started learning it from various online resources.
But still it confuses me when I need to implement it in my code.
For the given image which I have attached here, could anyone of you suggest me how should I start or where to start?
This is Azure function which I will be creating unit test for, framework/library I would prefer is Xunit and moq.
As mentioned in a comment, a good place to start when unit testing is looking at your code and identifying the different "paths" it can take and what the result of that path will be.
if (inventoryRequest != null)
{
// path 1
await _inventoryService.ProcessRequest(inventoryRequest);
_logger.LogInformation("HBSI Inventory Queue trigger function processed.");
}
else
{
// path 2
_logger.LogInformation("Unable to process HBSI Rate plan Queue.");
}
In your code, because of your if statement, there are 2 possible paths which will end in 2 different results = 2 unit tests.
Now you can start creating your unit tests but first you need to find out what you need to set up to be able to trigger your code.
private readonly ILogger _logger;
private readonly IInventoryService _inventoryService;
public InventoryServiceBusFunction(ILogger logger, IInventoryService inventoryService)
{
_logger = logger;
_inventoryService = inventoryService;
}
You have some dependencies being passed into your constructor with interfaces - great, this means we can mock them. We want to mock dependencies in unit tests because we want to control their behaviour for the tests. Also, mocking the dependencies negates any "real" behaviour the dependency might be performing i.e. database operations, API calls etc.
Using Moq we can mock the objects like so:
public class InventoryServiceBusFunctionTests
{
private readonly Mock<ILogger> _mockLogger = new Mock<ILogger>();
private readonly Mock<IInventoryService> _mockInventoryService = new Mock<IInventoryService>();
...
We will use these mocks later to make verifications on behaviour we expect to happen.
Next, we need to create an instance of the actual class we want to test.
// using a constructor in the test class will run this code before each test
public InventoryServiceBusFunctionTests()
{
// pass the mocked objects to initialize class
_inventoryServiceBusFunction = new InventoryServiceBusFunction(_mockLogger.Object, _mockInventoryService.Object);
}
Now that we have an instance of the InventoryServiceBusFunction class, we can use any of the public properties/methods in our tests.
[Fact]
public async Task GivenInventoryRequest_WhenFunctionRuns_ThenInventoryServiceProcessesRequest()
{
Now, remembering the paths from earlier, we can start to create the test cases. We can take the first path and create a [Fact] for it. You want to give your test case a meaningful name. I usually use the style of Given_When_Then to describe what is expected to happen.
Next, I usually add 3 comment sections to my test case:
// arrange
// act
// assert
This allows me to clearly see which parts of the test are doing what.
// act
await _inventoryServiceBusFunction.Run(inventoryRequest);
Next, I would fill in the \\ act section because this will tell me (via Intellisense) what I need to arrange. e.g. above, when hovering my mouse over the Run method, I can see that I need to pass an instance of InventoryRequest.
// arrange
var inventoryRequest = new InventoryRequest
{
Name = "abc123",
Quantity = 2,
Tags = new List<string>
{
"foo"
}
};
In the \\ arrange section, initialize an instance of the InventoryRequest class and set the properties. This can be any data as we aren't really interested in the data itself but more what happens when the code runs.
if (inventoryRequest != null)
{
// path 1
await _inventoryService.ProcessRequest(inventoryRequest);
_logger.LogInformation("HBSI Inventory Queue trigger function processed.");
}
Lastly, the \\ assert section. Here, we want to make assertions on what we expect to happen given the set up of the test. So given the InventoryRequest is not null, we expect the if to evaluate to true and we expect the _inventoryService.ProcessRequest(inventoryRequest) method to be executed.
// assert
_mockInventoryService
.Verify(x => x.ProcessRequest(It.Is<InventoryRequest>(ir => ir.Name == inventoryRequest.Name
&& ir.Quantity == inventoryRequest.Quantity
&& ir.Tags.Contains(inventoryRequest.Tags[0]))));
In Moq, we can use the .Verify() method on the mock object to assert that the method was called. We can use the It.Is<T> syntax to make assertions on the data that is passed to the method.
Here is the full test case for path 1:
[Fact]
public async Task GivenInventoryRequest_WhenFunctionRuns_ThenInventoryServiceProcessesRequest()
{
// arrange
var inventoryRequest = new InventoryRequest
{
Name = "abc123",
Quantity = 2,
Tags = new List<string>
{
"foo"
}
};
// act
await _inventoryServiceBusFunction.Run(inventoryRequest);
// assert
_mockInventoryService
.Verify(x => x.ProcessRequest(It.Is<InventoryRequest>(ir => ir.Name == inventoryRequest.Name
&& ir.Quantity == inventoryRequest.Quantity
&& ir.Tags.Contains(inventoryRequest.Tags[0]))));
}
Then for path 2, you are setting up the test so that the else condition is executed.
[Fact]
public async Task GivenInventoryRequestIsNull_WhenFunctionRuns_ThenInventoryServiceDoesNotProcessRequest()
{
// arrange
InventoryRequest inventoryRequest = null;
// act
await _inventoryServiceBusFunction.Run(inventoryRequest);
// assert
_mockInventoryService
.Verify(x => x.ProcessRequest(It.IsAny<InventoryRequest>()), Times.Never);
}
Note - in the \\ assert here, I am asserting that the await _inventoryService.ProcessRequest(inventoryRequest) method is never called. This is because you want the test to fail in this scenario as the method should only be executed in the if condition. You may also choose to verify that the logger method is called with the correct message.
I'm using NUnit 3 TestCaseData objects to feed test data to tests and Fluent Assertions library to check exceptions thrown.
Typically my TestCaseData object contains two parameters param1 and param2 used to create an instance of some object within the test and upon which I then invoke methods that should/should not throw exceptions, like this:
var subject = new Subject(param1, param2);
subject.Invoking(s => s.Add()).Should().NotThrow();
or
var subject = new Subject(param1, param2);
subject.Invoking(s => s.Add()).Should().Throw<ApplicationException>();
Is there a way to pass NotThrow() and Throw<ApplicationException>() parts as specific conditions in a third parameter in TestCaseData object to be used in the test? Basically I want to parameterize the test's expected result (it may be an exception of some type or no exception at all).
[TestCaseData] is meant for Test Case Data, not for assertions methods.
I would keep the NotThrow and Throw in separate tests to maintain readability.
If they share a lot of setup-logic, I would extract that into shared methods to reduce the size of the test method bodies.
TestCaseData accepts compile time values, whereas TestCaseSource generates them on runtime, which would be necessary to use Throw and NotThrow.
Here's a way to do it by misusing TestCaseSource.
The result is an unreadable test method, so please don't use this anywhere.
Anyway here goes:
[TestFixture]
public class ActionTests
{
private static IEnumerable<TestCaseData> ActionTestCaseData
{
get
{
yield return new TestCaseData((Action)(() => throw new Exception()), (Action<Action>)(act => act.Should().Throw<Exception>()));
yield return new TestCaseData((Action)(() => {}), (Action<Action>)(act => act.Should().NotThrow()));
}
}
[Test]
[TestCaseSource(typeof(ActionTests), nameof(ActionTestCaseData))]
public void Calculate_Success(Action act, Action<Action> assert)
{
assert(act);
}
}
I ended up using this:
using ExceptionResult = Action<System.Func<UserDetail>>;
[Test]
[TestCaseSource(typeof(UserEndpointTests), nameof(AddUserTestCases))]
public void User_Add(string creatorUsername, Role role, ExceptionResult result)
{
var endpoint = new UserEndpoint(creatorUsername);
var person = GeneratePerson();
var request = GenerateCreateUserRequest(person, role);
// Assertion comes here
result(endpoint.Invoking(e => e.Add(request)));
}
private static IEnumerable AddUserTestCases
{
get
{
yield return new TestCaseData(TestUserEmail, Role.User, new ExceptionResult(x => x.Should().Throw<ApplicationException>())
.SetName("{m} (Regular User => Regular User)")
.SetDescription("User with Regular User role cannot add any users.");
yield return new TestCaseData(TestAdminEmail, Role.Admin, new ExceptionResult(x => x.Should().NotThrow())
)
.SetName("{m} (Admin => Admin)")
.SetDescription("User with Admin role adds another user with Admin role.");
}
}
No big issues with readability, besides, SetName() and SetDescription() methods in the test case source help with that.
Version Akka.NET v1.3.8
Version Akka.TestKit.NUnit3 v1.3.2
Version NUnit v3.10.1
Platform Windows 10
I have an actor of the following kind:
public class BranchUsersActor : ReceiveActor
{
public BranchUsersActor()
{
Receive<UserBeingOnline>((online) =>
{
var userActorName = $"user_{online.UserId}";
if (Context.Child(userActorName).Equals(ActorRefs.Nobody))
{
var user = Context.ActorOf(UserActor.Props(online.UserId, online.BranchId), userActorName);
user.Tell(online);
}
});
}
public static Props Props(LoggingTags tags)
{
return Akka.Actor.Props.Create(() => new BranchUsersActor(tags));
}
}
When testing this actor, I expect that I will have a child actor.
I'm writing the next test to check this situation (using the NUnit framework):
[Test]
public void Test()
{
var branchUserActor = Sys.ActorOf(BranchUsersActor.Props());
branchUserActor.Tell(UserBeingOnline.Create(userId, branchId));
var expectedChildActor = Sys.ActorSelection($"{actorPath}/user_{userId.AkkaPrepare()}")
.ResolveOne(TimeSpan.FromSeconds(1)).Result;
Assert.IsNotNull(expectedChildActor);
}
I expect that within a second I will receive the child actor on the specified path, but I get ActorNotFoundExpection.
If I'm doing something like this:
[Test]
public void Test()
{
var branchUserActor = Sys.ActorOf(BranchUsersActor.Props());
branchUserActor.Tell(UserBeingOnline.Create(userId, branchId));
Task.Delay(100).ContinueWith(_ =>
{
var expectedChildActor = Sys.ActorSelection($"{actorPath}/user_{userId.AkkaPrepare()}")`enter code here`
.ResolveOne(TimeSpan.FromSeconds(1)).Result;
}
Assert.IsNotNull(expectedChildActor);
}
This works fine, but 1 of 10 times the test falls, because I get an ActorNotFoundException.
But I wonder why the first option does not work the way I expect?
Am I doing something wrong?
Thanks in advance for the answer.
branchUserActor.Tell(UserBeingOnline.Create(userId, branchId));
var expectedChildActor = Sys.ActorSelection($"{actorPath}/user_{userId.AkkaPrepare()}")
.ResolveOne(TimeSpan.FromSeconds(1)).Result;
The problem here is that when you're telling UserBeingOnline, you're triggering an asynchronous action - a message has been send to branchUserActor, but it may not have been processed right away. In the meantime you're calling resolve one, which tells actor system to find a child of branchUserActor - a child, which is not yet there, since the parent didn't handle the message yet.
You can use AwaitAssert(() => Assert.IsNotNull(ResolveChild())) method to work with that.
I'm trying to test a controller function...
I want to test a couple of things:
A) That it throws an invalid request exception when a certain argument is used
B) That it works correctly when the correct argument is made.
I've written some unit tests and those all seem cool. The only documentation I can find on this is http://book.cakephp.org/3.0/en/development/testing.html but the integration testing, whilst interesting and potentially useful, I can't seem to get how I am suppose to be implement it without using fixtures (which I don't want to do necessarily).
namespace App\Test\TestCase\Controller;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestCase;
class MusterControllerTest extends IntegrationTestCase
{
public function testIn()
{
$this->in();
$this->setExpectedException('Invalid request');
}
}
class MusterController extends AppController {
public $helpers = array('Address');
public function beforeFilter(Event $event) {
$this->Auth->allow('in');
$this->layout = 'blank';
$this->autoRender = false;
$this->loadComponent('Rule');
parent::beforeFilter($event);
}
public function in($param = null){
if (!$this->request->is(array('post', 'put')) || $this->request->data('proc')!='yada' || is_null($param)){
throw new NotFoundException(__('Invalid request'));
}
$this->processRequest($this->request->data('hit'), $this->request->data('proc'), $param);
}
Pointers appreciated.
The IntegrationTestCase class, as its name implies, is meant for integration testing. That is, it will be testing the interaction between the controller and any other class it uses for rendering a response.
There is another way of testing controller, which is more difficult to accomplish, but allows you to test controller methods in isolation:
public function testMyControllerMethod()
{
$request = $this->getMock('Cake\Network\Request');
$response = $this->getMock('Cake\Network\Response');
$controller = new MyController($request, $response);
$controller->startupProcess();
// Add some assertions and expectations here
// For example you could assing $controller->TableName to a mock class
// Call the method you want to test
$controller->myMethod('param1', 'param2');
}
Can anyone tell me why in the world the following test is not failing?
[Test]
public void uhh_what() {
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Expect(x => x.Notify()).Repeat.Once();
a.Notify();
a.Notify();
a.VerifyAllExpectations();
}
Really need a second pair of eyes to confirm I'm not crazy...now I'm worried that all my tests are unreliable.
There is already a thread on the RhinoMocks group.
GenerateMock creates a dynamic mock. The dynamic mock allows calls that are not specified (=expected). If this happens, it just returns null (or the default value of the return type).
Note: Repeat is a specification of the behaviour (like Stub), not the expectation even if specified in an expectation.
If you want to avoid having more then a certain number of calls, you could write:
[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Expect(x => x.Notify()).Repeat.Once();
a.Stub(x => x.Notify()).Throw(new InvalidOperationException("gotcha"));
a.Notify();
// this fails
a.Notify();
a.VerifyAllExpectations();
}
Or
[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Notify();
a.Notify();
// this fails
a.AssertWasCalled(
x => x.Notify(),
o => o.Repeat.Once());
}
When using GenerateMock (or with Dynamic Mocks in general) I always mentally insert the following:
a.Expect(x => x.Notify()).Repeat.*[AtLeast]*Once();