Skip to content

Commit e26c885

Browse files
Refactored the user details unit test to remove a lot of junk testing. #851
1 parent d68fb6b commit e26c885

5 files changed

Lines changed: 230 additions & 454 deletions

File tree

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Check testhelpers/fake_softlayer_session.go for all the fields that get recorded
157157

158158

159159

160-
### Fake Managers
160+
### Test Fakes
161+
161162

162163
CLI calls to manager functions need an entry in `plugin\testhelpers\fake_manager.go`
163164
Managers have a fake/test interface that is autogenerate with a program called [couterfieter](https://github.com/maxbrunsfeld/counterfeiter)
@@ -177,24 +178,37 @@ each manager and defined interface should have this line in it to be automatical
177178

178179
If you want to use the real manager but fixture API data, just initialize the manager like this in the CLI test
179180

180-
(filenames here is optional of course)
181+
This example is from `plugin\commands\account\invoice-detail_test.go`
181182
```go
182-
BeforeEach(func() {
183-
184-
filenames := []string{"getDatacenters_1",}
185-
fakeSLSession = testhelpers.NewFakeSoftlayerSession(filenames)
186-
OrderManager = managers.NewOrderManager(fakeSLSession)
187-
fakeUI = terminal.NewFakeUI()
188-
cmd = order.NewPlaceCommand(fakeUI, OrderManager, nil)
189-
cliCommand = cli.Command{
190-
Name: metadata.OrderPlaceMetaData().Name,
191-
Description: metadata.OrderPlaceMetaData().Description,
192-
Usage: metadata.OrderPlaceMetaData().Usage,
193-
Flags: metadata.OrderPlaceMetaData().Flags,
194-
Action: cmd.Run,
195-
}
196-
})
183+
var _ = Describe("<COMMAND> Tests", func() {
184+
var (
185+
fakeUI *terminal.FakeUI
186+
cliCommand *account.InvoiceDetailCommand
187+
fakeSession *session.Session
188+
slCommand *metadata.SoftlayerCommand
189+
fakeHandler *testhelpers.FakeTransportHandler
190+
)
191+
BeforeEach(func() {
192+
// Fake UI to capture output of comamnds
193+
fakeUI = terminal.NewFakeUI()
194+
// Fake session to handle loading data from testfixtures
195+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
196+
// Fake handler to control error generation
197+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
198+
// Real parent command, with fake UI and Fake Session being passed in
199+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
200+
// Real actual command
201+
cliCommand = account.NewInvoiceDetailCommand(slCommand)
202+
// Need to set output flag since its set manually in the parent command normally.
203+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
204+
})
205+
AfterEach(func() {
206+
// Clear API call logs and any errors that might have been set after every test
207+
fakeHandler.ClearApiCallLogs()
208+
fakeHandler.ClearErrors()
209+
})
197210
```
211+
`plugin\commands\user\details_test.go` is also a good example test file for CLI commands.
198212
199213
### `[no tests to run]`
200214
New commands needs a `command_test.go` file in the CLI directory.
@@ -224,8 +238,7 @@ fakeHandler = testhelpers.GetSessionHandler(fakeSession)
224238

225239
// Then in a BeforeEach for the specific test...
226240
BeforeEach(func() {
227-
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject",
228-
500, "Internal Server Error")
241+
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject", 500, "Internal Server Error")
229242
})
230243
```
231244

0 commit comments

Comments
 (0)