regulators: Simplify Device test cases

Simplify the gtest test cases for the Device class by making the
following enhancements:
* Convert the test suite to a test fixture class.  Automatically create
  the parent Chassis and System objects in the class constructor.
* Use auto when creating objects using std::make_unique() to eliminate
  redundant type declarations.
* Define a constant for the Device inventory path.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I86702a33c7ea7e1595ec251935e648c1c228d02b
diff --git a/phosphor-regulators/test/device_tests.cpp b/phosphor-regulators/test/device_tests.cpp
index 49a8a8d..be353f3 100644
--- a/phosphor-regulators/test/device_tests.cpp
+++ b/phosphor-regulators/test/device_tests.cpp
@@ -53,24 +53,52 @@
 using ::testing::Throw;
 using ::testing::TypedEq;
 
-static const std::string chassisInvPath{
-    "/xyz/openbmc_project/inventory/system/chassis"};
+class DeviceTests : public ::testing::Test
+{
+  public:
+    /**
+     * Constructor.
+     *
+     * Creates the Chassis and System objects needed for calling some Device
+     * methods.
+     */
+    DeviceTests() : ::testing::Test{}
+    {
+        // Create Chassis
+        auto chassis = std::make_unique<Chassis>(1, chassisInvPath);
+        this->chassis = chassis.get();
 
-TEST(DeviceTests, Constructor)
+        // Create System
+        std::vector<std::unique_ptr<Rule>> rules{};
+        std::vector<std::unique_ptr<Chassis>> chassisVec{};
+        chassisVec.emplace_back(std::move(chassis));
+        this->system =
+            std::make_unique<System>(std::move(rules), std::move(chassisVec));
+    }
+
+  protected:
+    const std::string deviceInvPath{
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2"};
+    const std::string chassisInvPath{
+        "/xyz/openbmc_project/inventory/system/chassis"};
+
+    // Note: This pointer does NOT need to be explicitly deleted.  The Chassis
+    // object is owned by the System object and will be automatically deleted.
+    Chassis* chassis{nullptr};
+
+    std::unique_ptr<System> system{};
+};
+
+TEST_F(DeviceTests, Constructor)
 {
     // Test where only required parameters are specified
     {
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
         i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(i2cInterface)};
+        Device device{"vdd_reg", true, deviceInvPath, std::move(i2cInterface)};
         EXPECT_EQ(device.getID(), "vdd_reg");
         EXPECT_EQ(device.isRegulator(), true);
-        EXPECT_EQ(
-            device.getFRU(),
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+        EXPECT_EQ(device.getFRU(), deviceInvPath);
         EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
         EXPECT_EQ(device.getPresenceDetection(), nullptr);
         EXPECT_EQ(device.getConfiguration(), nullptr);
@@ -87,7 +115,7 @@
         // Create PresenceDetection
         std::vector<std::unique_ptr<Action>> actions{};
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<PresenceDetection> presenceDetection =
+        auto presenceDetection =
             std::make_unique<PresenceDetection>(std::move(actions));
 
         // Create Configuration
@@ -95,7 +123,7 @@
         actions.clear();
         actions.push_back(std::make_unique<MockAction>());
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<Configuration> configuration =
+        auto configuration =
             std::make_unique<Configuration>(volts, std::move(actions));
 
         // Create PhaseFaultDetection
@@ -103,7 +131,7 @@
         actions.push_back(std::make_unique<MockAction>());
         actions.push_back(std::make_unique<MockAction>());
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<PhaseFaultDetection> phaseFaultDetection =
+        auto phaseFaultDetection =
             std::make_unique<PhaseFaultDetection>(std::move(actions));
 
         // Create vector of Rail objects
@@ -112,20 +140,17 @@
         rails.push_back(std::make_unique<Rail>("vdd1"));
 
         // Create Device
-        Device device{
-            "vdd_reg",
-            false,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface),
-            std::move(presenceDetection),
-            std::move(configuration),
-            std::move(phaseFaultDetection),
-            std::move(rails)};
+        Device device{"vdd_reg",
+                      false,
+                      deviceInvPath,
+                      std::move(i2cInterface),
+                      std::move(presenceDetection),
+                      std::move(configuration),
+                      std::move(phaseFaultDetection),
+                      std::move(rails)};
         EXPECT_EQ(device.getID(), "vdd_reg");
         EXPECT_EQ(device.isRegulator(), false);
-        EXPECT_EQ(
-            device.getFRU(),
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1");
+        EXPECT_EQ(device.getFRU(), deviceInvPath);
         EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
         EXPECT_NE(device.getPresenceDetection(), nullptr);
         EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
@@ -138,7 +163,7 @@
     }
 }
 
-TEST(DeviceTests, AddToIDMap)
+TEST_F(DeviceTests, AddToIDMap)
 {
     std::unique_ptr<PresenceDetection> presenceDetection{};
     std::unique_ptr<Configuration> configuration{};
@@ -150,15 +175,14 @@
     rails.push_back(std::make_unique<Rail>("vdd1"));
 
     // Create Device
-    Device device{
-        "vdd_reg",
-        false,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-        std::move(createI2CInterface()),
-        std::move(presenceDetection),
-        std::move(configuration),
-        std::move(phaseFaultDetection),
-        std::move(rails)};
+    Device device{"vdd_reg",
+                  false,
+                  deviceInvPath,
+                  std::move(createI2CInterface()),
+                  std::move(presenceDetection),
+                  std::move(configuration),
+                  std::move(phaseFaultDetection),
+                  std::move(rails)};
 
     // Add Device and Rail objects to an IDMap
     IDMap idMap{};
@@ -174,15 +198,13 @@
     EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument);
 }
 
-TEST(DeviceTests, ClearCache)
+TEST_F(DeviceTests, ClearCache)
 {
     // Test where Device does not contain a PresenceDetection object
     try
     {
-        Device device{
-            "vdd_reg", false,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())};
+        Device device{"vdd_reg", false, deviceInvPath,
+                      std::move(createI2CInterface())};
         device.clearCache();
     }
     catch (...)
@@ -194,89 +216,59 @@
     {
         // Create PresenceDetection
         std::vector<std::unique_ptr<Action>> actions{};
-        std::unique_ptr<PresenceDetection> presenceDetection =
+        auto presenceDetection =
             std::make_unique<PresenceDetection>(std::move(actions));
         PresenceDetection* presenceDetectionPtr = presenceDetection.get();
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface),
+                      std::move(presenceDetection)};
 
         // Cache presence value in PresenceDetection
         MockServices services{};
-        presenceDetectionPtr->execute(services, system, *chassisPtr,
-                                      *devicePtr);
+        presenceDetectionPtr->execute(services, *system, *chassis, device);
         EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value());
 
         // Clear cached data in Device
-        devicePtr->clearCache();
+        device.clearCache();
 
         // Verify presence value no longer cached in PresenceDetection
         EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
     }
 }
 
-TEST(DeviceTests, ClearErrorHistory)
+TEST_F(DeviceTests, ClearErrorHistory)
 {
     // Create SensorMonitoring.  Will fail with a DBus exception.
-    std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+    auto action = std::make_unique<MockAction>();
     EXPECT_CALL(*action, execute)
         .WillRepeatedly(Throw(TestSDBusError{"Unable to set sensor value"}));
     std::vector<std::unique_ptr<Action>> actions{};
     actions.emplace_back(std::move(action));
-    std::unique_ptr<SensorMonitoring> sensorMonitoring =
+    auto sensorMonitoring =
         std::make_unique<SensorMonitoring>(std::move(actions));
 
     // Create Rail
     std::unique_ptr<Configuration> configuration{};
-    std::unique_ptr<Rail> rail = std::make_unique<Rail>(
-        "vddr1", std::move(configuration), std::move(sensorMonitoring));
+    auto rail = std::make_unique<Rail>("vddr1", std::move(configuration),
+                                       std::move(sensorMonitoring));
 
     // Create Device that contains Rail
-    std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
-        std::make_unique<i2c::MockedI2CInterface>();
+    auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
     std::unique_ptr<PresenceDetection> presenceDetection{};
     std::unique_ptr<Configuration> deviceConfiguration{};
     std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
     std::vector<std::unique_ptr<Rail>> rails{};
     rails.emplace_back(std::move(rail));
-    std::unique_ptr<Device> device = std::make_unique<Device>(
-        "reg1", true,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-        std::move(i2cInterface), std::move(presenceDetection),
-        std::move(deviceConfiguration), std::move(phaseFaultDetection),
-        std::move(rails));
-    Device* devicePtr = device.get();
-
-    // Create Chassis that contains Device
-    std::vector<std::unique_ptr<Device>> devices{};
-    devices.emplace_back(std::move(device));
-    std::unique_ptr<Chassis> chassis =
-        std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-    Chassis* chassisPtr = chassis.get();
-
-    // Create System that contains Chassis
-    std::vector<std::unique_ptr<Rule>> rules{};
-    std::vector<std::unique_ptr<Chassis>> chassisVec{};
-    chassisVec.emplace_back(std::move(chassis));
-    System system{std::move(rules), std::move(chassisVec)};
+    Device device{"reg2",
+                  true,
+                  deviceInvPath,
+                  std::move(i2cInterface),
+                  std::move(presenceDetection),
+                  std::move(deviceConfiguration),
+                  std::move(phaseFaultDetection),
+                  std::move(rails)};
 
     // Create mock services
     MockServices services{};
@@ -301,27 +293,26 @@
     // times, and log one error.
     for (int i = 1; i <= 5; ++i)
     {
-        devicePtr->monitorSensors(services, system, *chassisPtr);
+        device.monitorSensors(services, *system, *chassis);
     }
 
     // Clear error history
-    devicePtr->clearErrorHistory();
+    device.clearErrorHistory();
 
     // Monitor sensors 5 times again.  Should fail every time, write to journal
     // 3 times, and log one error.
     for (int i = 1; i <= 5; ++i)
     {
-        devicePtr->monitorSensors(services, system, *chassisPtr);
+        device.monitorSensors(services, *system, *chassis);
     }
 }
 
-TEST(DeviceTests, Close)
+TEST_F(DeviceTests, Close)
 {
     // Test where works: I2C interface is not open
     {
         // Create mock I2CInterface
-        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
-            std::make_unique<i2c::MockedI2CInterface>();
+        auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
         EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
         EXPECT_CALL(*i2cInterface, close).Times(0);
 
@@ -333,10 +324,7 @@
             .Times(0);
 
         // Create Device
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(i2cInterface)};
+        Device device{"vdd_reg", true, deviceInvPath, std::move(i2cInterface)};
 
         // Close Device
         device.close(services);
@@ -345,8 +333,7 @@
     // Test where works: I2C interface is open
     {
         // Create mock I2CInterface
-        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
-            std::make_unique<i2c::MockedI2CInterface>();
+        auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
         EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
         EXPECT_CALL(*i2cInterface, close).Times(1);
 
@@ -358,10 +345,7 @@
             .Times(0);
 
         // Create Device
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(i2cInterface)};
+        Device device{"vdd_reg", true, deviceInvPath, std::move(i2cInterface)};
 
         // Close Device
         device.close(services);
@@ -370,8 +354,7 @@
     // Test where fails: closing I2C interface fails
     {
         // Create mock I2CInterface
-        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
-            std::make_unique<i2c::MockedI2CInterface>();
+        auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
         EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
         EXPECT_CALL(*i2cInterface, close)
             .Times(1)
@@ -394,17 +377,14 @@
             .Times(1);
 
         // Create Device
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(i2cInterface)};
+        Device device{"vdd_reg", true, deviceInvPath, std::move(i2cInterface)};
 
         // Close Device
         device.close(services);
     }
 }
 
-TEST(DeviceTests, Configure)
+TEST_F(DeviceTests, Configure)
 {
     // Test where device is not present
     {
@@ -415,46 +395,39 @@
         EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
 
         // Create PresenceDetection.  Indicates device is not present.
-        std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
-        EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
-        std::vector<std::unique_ptr<Action>> presActions{};
-        presActions.emplace_back(std::move(presAction));
-        std::unique_ptr<PresenceDetection> presenceDetection =
-            std::make_unique<PresenceDetection>(std::move(presActions));
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        {
+            auto action = std::make_unique<MockAction>();
+            EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+            std::vector<std::unique_ptr<Action>> actions{};
+            actions.emplace_back(std::move(action));
+            presenceDetection =
+                std::make_unique<PresenceDetection>(std::move(actions));
+        }
 
         // Create Configuration.  Action inside it should not be executed.
-        std::optional<double> volts{};
-        std::unique_ptr<MockAction> confAction = std::make_unique<MockAction>();
-        EXPECT_CALL(*confAction, execute).Times(0);
-        std::vector<std::unique_ptr<Action>> confActions{};
-        confActions.emplace_back(std::move(confAction));
-        std::unique_ptr<Configuration> configuration =
-            std::make_unique<Configuration>(volts, std::move(confActions));
+        std::unique_ptr<Configuration> configuration{};
+        {
+            std::optional<double> volts{};
+            auto action = std::make_unique<MockAction>();
+            EXPECT_CALL(*action, execute).Times(0);
+            std::vector<std::unique_ptr<Action>> actions{};
+            actions.emplace_back(std::move(action));
+            configuration =
+                std::make_unique<Configuration>(volts, std::move(actions));
+        }
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection),
-            std::move(configuration));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2",
+                      true,
+                      deviceInvPath,
+                      std::move(i2cInterface),
+                      std::move(presenceDetection),
+                      std::move(configuration)};
 
         // Call configure().  Should do nothing.
-        devicePtr->configure(services, system, *chassisPtr);
+        device.configure(services, *system, *chassis);
     }
 
     // Test where Configuration and Rails were not specified in constructor
@@ -467,27 +440,10 @@
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface)};
 
         // Call configure().
-        devicePtr->configure(services, system, *chassisPtr);
+        device.configure(services, *system, *chassis);
     }
 
     // Test where Configuration and Rails were specified in constructor
@@ -499,7 +455,7 @@
         // and log a debug message.
         MockServices services{};
         MockJournal& journal = services.getMockJournal();
-        EXPECT_CALL(journal, logDebug("Configuring reg1")).Times(1);
+        EXPECT_CALL(journal, logDebug("Configuring reg2")).Times(1);
         EXPECT_CALL(journal, logDebug("Configuring vdd0: volts=1.300000"))
             .Times(1);
         EXPECT_CALL(journal, logDebug("Configuring vio0: volts=3.200000"))
@@ -510,15 +466,15 @@
         {
             // Create Configuration for Rail
             std::optional<double> volts{1.3};
-            std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+            auto action = std::make_unique<MockAction>();
             EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
             std::vector<std::unique_ptr<Action>> actions{};
             actions.emplace_back(std::move(action));
-            std::unique_ptr<Configuration> configuration =
+            auto configuration =
                 std::make_unique<Configuration>(volts, std::move(actions));
 
             // Create Rail
-            std::unique_ptr<Rail> rail =
+            auto rail =
                 std::make_unique<Rail>("vdd0", std::move(configuration));
             rails.emplace_back(std::move(rail));
         }
@@ -527,66 +483,52 @@
         {
             // Create Configuration for Rail
             std::optional<double> volts{3.2};
-            std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+            auto action = std::make_unique<MockAction>();
             EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
             std::vector<std::unique_ptr<Action>> actions{};
             actions.emplace_back(std::move(action));
-            std::unique_ptr<Configuration> configuration =
+            auto configuration =
                 std::make_unique<Configuration>(volts, std::move(actions));
 
             // Create Rail
-            std::unique_ptr<Rail> rail =
+            auto rail =
                 std::make_unique<Rail>("vio0", std::move(configuration));
             rails.emplace_back(std::move(rail));
         }
 
         // Create Configuration for Device
         std::optional<double> volts{};
-        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        auto action = std::make_unique<MockAction>();
         EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
         std::vector<std::unique_ptr<Action>> actions{};
         actions.emplace_back(std::move(action));
-        std::unique_ptr<Configuration> configuration =
+        auto configuration =
             std::make_unique<Configuration>(volts, std::move(actions));
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
         std::unique_ptr<PresenceDetection> presenceDetection{};
         std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection),
-            std::move(configuration), std::move(phaseFaultDetection),
-            std::move(rails));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2",
+                      true,
+                      deviceInvPath,
+                      std::move(i2cInterface),
+                      std::move(presenceDetection),
+                      std::move(configuration),
+                      std::move(phaseFaultDetection),
+                      std::move(rails)};
 
         // Call configure().
-        devicePtr->configure(services, system, *chassisPtr);
+        device.configure(services, *system, *chassis);
     }
 }
 
-TEST(DeviceTests, GetConfiguration)
+TEST_F(DeviceTests, GetConfiguration)
 {
     // Test where Configuration was not specified in constructor
     {
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())};
+        Device device{"vdd_reg", true, deviceInvPath,
+                      std::move(createI2CInterface())};
         EXPECT_EQ(device.getConfiguration(), nullptr);
     }
 
@@ -599,17 +541,16 @@
         std::vector<std::unique_ptr<Action>> actions{};
         actions.push_back(std::make_unique<MockAction>());
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<Configuration> configuration =
+        auto configuration =
             std::make_unique<Configuration>(volts, std::move(actions));
 
         // Create Device
-        Device device{
-            "vdd_reg",
-            true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface()),
-            std::move(presenceDetection),
-            std::move(configuration)};
+        Device device{"vdd_reg",
+                      true,
+                      deviceInvPath,
+                      std::move(createI2CInterface()),
+                      std::move(presenceDetection),
+                      std::move(configuration)};
         EXPECT_NE(device.getConfiguration(), nullptr);
         EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true);
         EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2);
@@ -617,44 +558,34 @@
     }
 }
 
-TEST(DeviceTests, GetFRU)
+TEST_F(DeviceTests, GetFRU)
 {
-    Device device{
-        "vdd_reg", true,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-        std::move(createI2CInterface())};
-    EXPECT_EQ(device.getFRU(),
-              "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+    Device device{"vdd_reg", true, deviceInvPath,
+                  std::move(createI2CInterface())};
+    EXPECT_EQ(device.getFRU(), deviceInvPath);
 }
 
-TEST(DeviceTests, GetI2CInterface)
+TEST_F(DeviceTests, GetI2CInterface)
 {
     std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
     i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
-    Device device{
-        "vdd_reg", true,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-        std::move(i2cInterface)};
+    Device device{"vdd_reg", true, deviceInvPath, std::move(i2cInterface)};
     EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
 }
 
-TEST(DeviceTests, GetID)
+TEST_F(DeviceTests, GetID)
 {
-    Device device{
-        "vdd_reg", false,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-        std::move(createI2CInterface())};
+    Device device{"vdd_reg", false, deviceInvPath,
+                  std::move(createI2CInterface())};
     EXPECT_EQ(device.getID(), "vdd_reg");
 }
 
-TEST(DeviceTests, GetPhaseFaultDetection)
+TEST_F(DeviceTests, GetPhaseFaultDetection)
 {
     // Test where PhaseFaultDetection was not specified in constructor
     {
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())};
+        Device device{"vdd_reg", true, deviceInvPath,
+                      std::move(createI2CInterface())};
         EXPECT_EQ(device.getPhaseFaultDetection(), nullptr);
     }
 
@@ -663,33 +594,30 @@
         // Create PhaseFaultDetection
         std::vector<std::unique_ptr<Action>> actions{};
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<PhaseFaultDetection> phaseFaultDetection =
+        auto phaseFaultDetection =
             std::make_unique<PhaseFaultDetection>(std::move(actions));
 
         // Create Device
         std::unique_ptr<PresenceDetection> presenceDetection{};
         std::unique_ptr<Configuration> configuration{};
-        Device device{
-            "vdd_reg",
-            false,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface()),
-            std::move(presenceDetection),
-            std::move(configuration),
-            std::move(phaseFaultDetection)};
+        Device device{"vdd_reg",
+                      false,
+                      deviceInvPath,
+                      std::move(createI2CInterface()),
+                      std::move(presenceDetection),
+                      std::move(configuration),
+                      std::move(phaseFaultDetection)};
         EXPECT_NE(device.getPhaseFaultDetection(), nullptr);
         EXPECT_EQ(device.getPhaseFaultDetection()->getActions().size(), 1);
     }
 }
 
-TEST(DeviceTests, GetPresenceDetection)
+TEST_F(DeviceTests, GetPresenceDetection)
 {
     // Test where PresenceDetection was not specified in constructor
     {
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())};
+        Device device{"vdd_reg", true, deviceInvPath,
+                      std::move(createI2CInterface())};
         EXPECT_EQ(device.getPresenceDetection(), nullptr);
     }
 
@@ -698,27 +626,24 @@
         // Create PresenceDetection
         std::vector<std::unique_ptr<Action>> actions{};
         actions.push_back(std::make_unique<MockAction>());
-        std::unique_ptr<PresenceDetection> presenceDetection =
+        auto presenceDetection =
             std::make_unique<PresenceDetection>(std::move(actions));
 
         // Create Device
-        Device device{
-            "vdd_reg", false,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface()), std::move(presenceDetection)};
+        Device device{"vdd_reg", false, deviceInvPath,
+                      std::move(createI2CInterface()),
+                      std::move(presenceDetection)};
         EXPECT_NE(device.getPresenceDetection(), nullptr);
         EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
     }
 }
 
-TEST(DeviceTests, GetRails)
+TEST_F(DeviceTests, GetRails)
 {
     // Test where no rails were specified in constructor
     {
-        Device device{
-            "vdd_reg", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())};
+        Device device{"vdd_reg", true, deviceInvPath,
+                      std::move(createI2CInterface())};
         EXPECT_EQ(device.getRails().size(), 0);
     }
 
@@ -734,140 +659,88 @@
         rails.push_back(std::make_unique<Rail>("vdd1"));
 
         // Create Device
-        Device device{
-            "vdd_reg",
-            false,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface()),
-            std::move(presenceDetection),
-            std::move(configuration),
-            std::move(phaseFaultDetection),
-            std::move(rails)};
+        Device device{"vdd_reg",
+                      false,
+                      deviceInvPath,
+                      std::move(createI2CInterface()),
+                      std::move(presenceDetection),
+                      std::move(configuration),
+                      std::move(phaseFaultDetection),
+                      std::move(rails)};
         EXPECT_EQ(device.getRails().size(), 2);
         EXPECT_EQ(device.getRails()[0]->getID(), "vdd0");
         EXPECT_EQ(device.getRails()[1]->getID(), "vdd1");
     }
 }
 
-TEST(DeviceTests, IsPresent)
+TEST_F(DeviceTests, IsPresent)
 {
     // Test where PresenceDetection not specified in constructor
     {
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface)};
 
         // Create MockServices
         MockServices services{};
 
         // Since no PresenceDetection defined, isPresent() should return true
-        EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
+        EXPECT_TRUE(device.isPresent(services, *system, *chassis));
     }
 
     // Test where PresenceDetection was specified in constructor: Is present
     {
         // Create PresenceDetection.  Indicates device is present.
-        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        auto action = std::make_unique<MockAction>();
         EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
         std::vector<std::unique_ptr<Action>> actions{};
         actions.emplace_back(std::move(action));
-        std::unique_ptr<PresenceDetection> presenceDetection =
+        auto presenceDetection =
             std::make_unique<PresenceDetection>(std::move(actions));
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface),
+                      std::move(presenceDetection)};
 
         // Create MockServices
         MockServices services{};
 
         // PresenceDetection::execute() and isPresent() should return true
-        EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
+        EXPECT_TRUE(device.isPresent(services, *system, *chassis));
     }
 
     // Test where PresenceDetection was specified in constructor: Is not present
     {
         // Create PresenceDetection.  Indicates device is not present.
-        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        auto action = std::make_unique<MockAction>();
         EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
         std::vector<std::unique_ptr<Action>> actions{};
         actions.emplace_back(std::move(action));
-        std::unique_ptr<PresenceDetection> presenceDetection =
+        auto presenceDetection =
             std::make_unique<PresenceDetection>(std::move(actions));
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface),
+                      std::move(presenceDetection)};
 
         // Create MockServices
         MockServices services{};
 
         // PresenceDetection::execute() and isPresent() should return false
-        EXPECT_FALSE(devicePtr->isPresent(services, system, *chassisPtr));
+        EXPECT_FALSE(device.isPresent(services, *system, *chassis));
     }
 }
 
-TEST(DeviceTests, IsRegulator)
+TEST_F(DeviceTests, IsRegulator)
 {
-    Device device{
-        "vdd_reg", false,
-        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
-        std::move(createI2CInterface())};
+    Device device{"vdd_reg", false, deviceInvPath,
+                  std::move(createI2CInterface())};
     EXPECT_EQ(device.isRegulator(), false);
 }
 
-TEST(DeviceTests, MonitorSensors)
+TEST_F(DeviceTests, MonitorSensors)
 {
     // Test where device is not present
     {
@@ -879,25 +752,31 @@
         EXPECT_CALL(sensors, endRail).Times(0);
 
         // Create SensorMonitoring.  Action inside it should not be executed.
-        std::unique_ptr<MockAction> sensAction = std::make_unique<MockAction>();
-        EXPECT_CALL(*sensAction, execute).Times(0);
-        std::vector<std::unique_ptr<Action>> sensActions{};
-        sensActions.emplace_back(std::move(sensAction));
-        std::unique_ptr<SensorMonitoring> sensorMonitoring =
-            std::make_unique<SensorMonitoring>(std::move(sensActions));
+        std::unique_ptr<SensorMonitoring> sensorMonitoring{};
+        {
+            auto action = std::make_unique<MockAction>();
+            EXPECT_CALL(*action, execute).Times(0);
+            std::vector<std::unique_ptr<Action>> actions{};
+            actions.emplace_back(std::move(action));
+            sensorMonitoring =
+                std::make_unique<SensorMonitoring>(std::move(actions));
+        }
 
         // Create Rail
         std::unique_ptr<Configuration> configuration{};
-        std::unique_ptr<Rail> rail = std::make_unique<Rail>(
-            "vddr1", std::move(configuration), std::move(sensorMonitoring));
+        auto rail = std::make_unique<Rail>("vddr1", std::move(configuration),
+                                           std::move(sensorMonitoring));
 
         // Create PresenceDetection.  Indicates device is not present.
-        std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
-        EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
-        std::vector<std::unique_ptr<Action>> presActions{};
-        presActions.emplace_back(std::move(presAction));
-        std::unique_ptr<PresenceDetection> presenceDetection =
-            std::make_unique<PresenceDetection>(std::move(presActions));
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        {
+            auto action = std::make_unique<MockAction>();
+            EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+            std::vector<std::unique_ptr<Action>> actions{};
+            actions.emplace_back(std::move(action));
+            presenceDetection =
+                std::make_unique<PresenceDetection>(std::move(actions));
+        }
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
@@ -905,29 +784,17 @@
         std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
         std::vector<std::unique_ptr<Rail>> rails{};
         rails.emplace_back(std::move(rail));
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection),
-            std::move(deviceConfiguration), std::move(phaseFaultDetection),
-            std::move(rails));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2",
+                      true,
+                      deviceInvPath,
+                      std::move(i2cInterface),
+                      std::move(presenceDetection),
+                      std::move(deviceConfiguration),
+                      std::move(phaseFaultDetection),
+                      std::move(rails)};
 
         // Call monitorSensors().  Should do nothing.
-        devicePtr->monitorSensors(services, system, *chassisPtr);
+        device.monitorSensors(services, *system, *chassis);
     }
 
     // Test where Rails were not specified in constructor
@@ -941,27 +808,10 @@
 
         // Create Device
         std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2", true, deviceInvPath, std::move(i2cInterface)};
 
         // Call monitorSensors().  Should do nothing.
-        devicePtr->monitorSensors(services, system, *chassisPtr);
+        device.monitorSensors(services, *system, *chassis);
     }
 
     // Test where Rails were specified in constructor
@@ -969,15 +819,9 @@
         // Create mock services.  Set Sensors service expectations.
         MockServices services{};
         MockSensors& sensors = services.getMockSensors();
-        EXPECT_CALL(sensors, startRail("vdd0",
-                                       "/xyz/openbmc_project/inventory/system/"
-                                       "chassis/motherboard/reg1",
-                                       chassisInvPath))
+        EXPECT_CALL(sensors, startRail("vdd0", deviceInvPath, chassisInvPath))
             .Times(1);
-        EXPECT_CALL(sensors, startRail("vio0",
-                                       "/xyz/openbmc_project/inventory/system/"
-                                       "chassis/motherboard/reg1",
-                                       chassisInvPath))
+        EXPECT_CALL(sensors, startRail("vio0", deviceInvPath, chassisInvPath))
             .Times(1);
         EXPECT_CALL(sensors, setValue).Times(0);
         EXPECT_CALL(sensors, endRail(false)).Times(2);
@@ -987,34 +831,34 @@
         // Create Rail vdd0
         {
             // Create SensorMonitoring for Rail
-            std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+            auto action = std::make_unique<MockAction>();
             EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
             std::vector<std::unique_ptr<Action>> actions{};
             actions.emplace_back(std::move(action));
-            std::unique_ptr<SensorMonitoring> sensorMonitoring =
+            auto sensorMonitoring =
                 std::make_unique<SensorMonitoring>(std::move(actions));
 
             // Create Rail
             std::unique_ptr<Configuration> configuration{};
-            std::unique_ptr<Rail> rail = std::make_unique<Rail>(
-                "vdd0", std::move(configuration), std::move(sensorMonitoring));
+            auto rail = std::make_unique<Rail>("vdd0", std::move(configuration),
+                                               std::move(sensorMonitoring));
             rails.emplace_back(std::move(rail));
         }
 
         // Create Rail vio0
         {
             // Create SensorMonitoring for Rail
-            std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+            auto action = std::make_unique<MockAction>();
             EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
             std::vector<std::unique_ptr<Action>> actions{};
             actions.emplace_back(std::move(action));
-            std::unique_ptr<SensorMonitoring> sensorMonitoring =
+            auto sensorMonitoring =
                 std::make_unique<SensorMonitoring>(std::move(actions));
 
             // Create Rail
             std::unique_ptr<Configuration> configuration{};
-            std::unique_ptr<Rail> rail = std::make_unique<Rail>(
-                "vio0", std::move(configuration), std::move(sensorMonitoring));
+            auto rail = std::make_unique<Rail>("vio0", std::move(configuration),
+                                               std::move(sensorMonitoring));
             rails.emplace_back(std::move(rail));
         }
 
@@ -1023,28 +867,16 @@
         std::unique_ptr<PresenceDetection> presenceDetection{};
         std::unique_ptr<Configuration> configuration{};
         std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
-        std::unique_ptr<Device> device = std::make_unique<Device>(
-            "reg1", true,
-            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
-            std::move(i2cInterface), std::move(presenceDetection),
-            std::move(configuration), std::move(phaseFaultDetection),
-            std::move(rails));
-        Device* devicePtr = device.get();
-
-        // Create Chassis that contains Device
-        std::vector<std::unique_ptr<Device>> devices{};
-        devices.emplace_back(std::move(device));
-        std::unique_ptr<Chassis> chassis =
-            std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
-        Chassis* chassisPtr = chassis.get();
-
-        // Create System that contains Chassis
-        std::vector<std::unique_ptr<Rule>> rules{};
-        std::vector<std::unique_ptr<Chassis>> chassisVec{};
-        chassisVec.emplace_back(std::move(chassis));
-        System system{std::move(rules), std::move(chassisVec)};
+        Device device{"reg2",
+                      true,
+                      deviceInvPath,
+                      std::move(i2cInterface),
+                      std::move(presenceDetection),
+                      std::move(configuration),
+                      std::move(phaseFaultDetection),
+                      std::move(rails)};
 
         // Call monitorSensors().  Should monitor sensors in both rails.
-        devicePtr->monitorSensors(services, system, *chassisPtr);
+        device.monitorSensors(services, *system, *chassis);
     }
 }