github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/requirements/config_refreshing_requirement_test.go (about) 1 package requirements_test 2 3 import ( 4 . "code.cloudfoundry.org/cli/cf/requirements" 5 6 "errors" 7 8 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("ConfigRefreshingRequirement", func() { 14 var ( 15 r Requirement 16 underlyingRequirement *requirementsfakes.FakeRequirement 17 configRefresher *requirementsfakes.FakeConfigRefresher 18 ) 19 20 BeforeEach(func() { 21 underlyingRequirement = new(requirementsfakes.FakeRequirement) 22 configRefresher = new(requirementsfakes.FakeConfigRefresher) 23 r = NewConfigRefreshingRequirement(underlyingRequirement, configRefresher) 24 }) 25 26 Describe("Execute", func() { 27 It("tries to execute the underlying requirement", func() { 28 underlyingRequirement.ExecuteReturns(nil) 29 30 r.Execute() 31 32 Expect(underlyingRequirement.ExecuteCallCount()).To(Equal(1)) 33 Expect(configRefresher.RefreshCallCount()).To(Equal(0)) 34 }) 35 36 Context("when the underlying requirement fails", func() { 37 It("refreshes the config", func() { 38 underlyingRequirement.ExecuteReturns(errors.New("TERRIBLE THINGS")) 39 40 r.Execute() 41 42 Expect(configRefresher.RefreshCallCount()).To(Equal(1)) 43 }) 44 45 It("returns the value of calling execute on the underlying requirement again", func() { 46 var count int 47 disaster := errors.New("TERRIBLE THINGS") 48 secondaryDisaster := errors.New("REALLY TERRIBLE THINGS") 49 50 underlyingRequirement.ExecuteStub = func() error { 51 if count == 0 { 52 count++ 53 return disaster 54 } 55 return secondaryDisaster 56 } 57 58 err := r.Execute() 59 60 Expect(underlyingRequirement.ExecuteCallCount()).To(Equal(2)) 61 Expect(err).To(Equal(secondaryDisaster)) 62 }) 63 64 Context("if config refresh fails", func() { 65 It("returns the error", func() { 66 underlyingRequirement.ExecuteReturns(errors.New("TERRIBLE THINGS")) 67 oops := errors.New("Can't get things") 68 configRefresher.RefreshReturns(nil, oops) 69 70 err := r.Execute() 71 Expect(err).To(Equal(oops)) 72 73 Expect(underlyingRequirement.ExecuteCallCount()).To(Equal(1)) 74 }) 75 }) 76 }) 77 }) 78 })