Describe("Turning light on", func() { BeforeEach(func() { u, err := url.Parse("/?state=on") Expect(err).ShouldNot(HaveOccurred()) dummyRequest.URL = u expectedLightState.StateKnown = true expectedLightState.LightOn = true expectedReturn, err = json.Marshal(expectedLightState) Expect(err).NotTo(HaveOccurred()) }) Context("When turning on light commands returns with error", func() { BeforeEach(func() { expectedError := errors.New(fmt.Sprintf("gpio write error")) fakeGpio.WriteHighReturns(expectedError) expectedLightState.StateKnown = false expectedLightState.LightOn = false expectedLightState.ErrorMsg = expectedError.Error() expectedReturn, err = json.Marshal(expectedLightState) Expect(err).NotTo(HaveOccurred()) }) It("Should write high to light pin", func() { lh.HandleSet(fakeResponseWriter, dummyRequest) Expect(fakeGpio.WriteHighCallCount()).To(Equal(1)) actualGpioPin := fakeGpio.WriteHighArgsForCall(0) Expect(actualGpioPin).To(Equal(gpioLightPin))
Expect(actualHighPin).To(Equal(gpioDoorPin)) actualLowPin := fakeGpio.WriteLowArgsForCall(0) Expect(actualLowPin).To(Equal(gpioDoorPin)) }) It("Should return 'door toggled'", func() { dh.HandleToggle(fakeResponseWriter, dummyRequest) Expect(fakeResponseWriter.WriteCallCount()).To(Equal(1)) Expect(fakeResponseWriter.WriteArgsForCall(0)).To(Equal([]byte("door toggled"))) }) }) Context("When writing high returns with errors", func() { BeforeEach(func() { fakeGpio.WriteHighReturns(errors.New("gpio error")) }) It("Should not sleep or execute further gpio commands", func() { dh.HandleToggle(fakeResponseWriter, dummyRequest) Expect(fakeOSHelper.SleepCallCount()).To(Equal(0)) Expect(fakeGpio.WriteHighCallCount()).To(Equal(1)) actualHighPin := fakeGpio.WriteHighArgsForCall(0) Expect(actualHighPin).To(Equal(gpioDoorPin)) }) It("Should return 'error - door not toggled'", func() { dh.HandleToggle(fakeResponseWriter, dummyRequest) Expect(fakeResponseWriter.WriteCallCount()).To(Equal(1))