Skip to content

Commit

Permalink
test: covers the themes list command
Browse files Browse the repository at this point in the history
  • Loading branch information
aubm committed May 18, 2017
1 parent d2a25e4 commit 0a05d53
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ doc.html
doc.md
collection.json
.cover
*.coverprofile
13 changes: 13 additions & 0 deletions commands/commands_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package commands_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestCommands(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Commands Suite")
}
89 changes: 89 additions & 0 deletions commands/list_themes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package commands_test

import (
"bytes"
"errors"

. "github.com/aubm/postmanerator/commands"
"github.com/aubm/postmanerator/configuration"
. "github.com/aubm/postmanerator/themes/tests"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ListThemes", func() {

var (
mockStdOut *bytes.Buffer
mockThemesManager *MockThemesManager
listThemesCommand *ListThemes
)

BeforeEach(func() {
mockStdOut = new(bytes.Buffer)
mockThemesManager = &MockThemesManager{}
mockConfig := &configuration.Configuration{Out: mockStdOut}
listThemesCommand = &ListThemes{
Config: mockConfig,
Themes: mockThemesManager,
}
})

Describe("Is", func() {

It("should be OK", func() {
Expect(listThemesCommand.Is("cmd_themes_list")).To(BeTrue())
})

It("should be KO", func() {
Expect(listThemesCommand.Is("cmd_default")).To(BeFalse())
})

})

Describe("Do", func() {

var returnedError error

JustBeforeEach(func() {
returnedError = listThemesCommand.Do()
})

Context("when the manager responds with a list of themes", func() {

BeforeEach(func() {
mockThemesManager.On("List").Return([]string{"default", "markdown"}, nil)
})

It("should not return an error", func() {
Expect(returnedError).To(BeNil())
})

It("should print the themes in the output", func() {
Expect(mockStdOut.String()).To(Equal("default\nmarkdown\n"))
})

})

Context("when the manager responds with an error", func() {

var managerError = errors.New("something bad happened")

BeforeEach(func() {
mockThemesManager.On("List").Return([]string{}, managerError)
})

It("should return an error", func() {
Expect(returnedError).NotTo(BeNil())
Expect(returnedError).To(Equal(managerError))
})

It("should not write anything in the output", func() {
Expect(mockStdOut.String()).To(BeZero())
})

})

})

})
12 changes: 12 additions & 0 deletions themes/tests/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tests

import "github.com/stretchr/testify/mock"

type MockThemesManager struct {
mock.Mock
}

func (m *MockThemesManager) List() ([]string, error) {
args := m.Called()
return args.Get(0).([]string), args.Error(1)
}

0 comments on commit 0a05d53

Please sign in to comment.