Should I write unit tests which verify that constructor is throwing in simple cases when a nullptr was provided instead of a shared_ptr?
Let's say I have the following class:
#pragma once
#include "SharedItemsListInteractorInput.hpp"
#include "SharedItemsListInteractorOutput.hpp"
#include "Store.hpp"
namespace ri {
class ItemsListInteractor : public SharedItemsListInteractorInput {
private:
std::shared_ptr<Store> _store;
std::shared_ptr<SharedItemsListInteractorOutput> _output;
public:
ItemsListInteractor(
const std::shared_ptr<Store> &store,
const std::shared_ptr<SharedItemsListInteractorOutput> &output
);
void fetchItems() override;
};
}
Implementation:
#include "ItemsListInteractor.hpp"
#include <iostream>
namespace ri {
ItemsListInteractor::ItemsListInteractor(
const std::shared_ptr<Store> &store,
const std::shared_ptr<SharedItemsListInteractorOutput> &output
) {
if (store == nullptr) {
throw std::invalid_argument("Store should not be an std::nullptr");
}
if (output == nullptr) {
throw std::invalid_argument("Output should not be an std::nullptr");
}
_store = store;
_output = output;
}
void ItemsListInteractor::fetchItems() {
_store->getItems(0, [=] (std::vector<SharedItem> items, bool nextPage) {
if (_output != nullptr) {
_output->didFetchItems(items, nextPage);
}
});
}
}
Construction tests:
#include "ItemsListInteractor.hpp"
#include "catch.hpp"
#include "fakeit.hpp"
using namespace Catch;
using namespace fakeit;
using namespace ri;
TEST_CASE( "items list interactor", "[ItemsListInteractor]" ) {
SECTION( "no store" ) {
Mock<SharedItemsListInteractorOutput> outputMock;
Fake(Dtor(outputMock));
auto output = std::shared_ptr<SharedItemsListInteractorOutput>(&outputMock.get());
REQUIRE_THROWS_AS(ItemsListInteractor(nullptr, output), std::invalid_argument);
}
SECTION( "no output" ) {
Mock<Store> storeMock;
Fake(Dtor(storeMock));
auto store = std::shared_ptr<Store>(&storeMock.get());
REQUIRE_THROWS_AS(ItemsListInteractor(store, nullptr), std::invalid_argument);
}
}
Feels like writing constructor tests, in this case, brings too much boilerplate code.