Exemplo n.º 1
0
				fakeGpio.ReadReturns("", errors.New("gpio read error"))
				expectedLightState.StateKnown = false
				expectedReturn, err = json.Marshal(expectedLightState)
				Expect(err).NotTo(HaveOccurred())
			})

			It("Should read from light pin", func() {
				lh.HandleGet(fakeResponseWriter, dummyRequest)
				Expect(fakeGpio.ReadCallCount()).To(Equal(1))
				Expect(fakeGpio.ReadArgsForCall(0)).To(Equal(gpioLightPin))
			})

			It("Should return unknown light state", func() {
				lh.HandleGet(fakeResponseWriter, dummyRequest)
				Expect(fakeResponseWriter.WriteCallCount()).To(Equal(1))
				Expect(fakeResponseWriter.WriteArgsForCall(0)).To(Equal(expectedReturn))
			})

			It("Should respond with HTTP status code 503", func() {
				lh.HandleGet(fakeResponseWriter, dummyRequest)
				Expect(fakeResponseWriter.WriteHeaderCallCount()).To(Equal(1))
				Expect(fakeResponseWriter.WriteHeaderArgsForCall(0)).To(Equal(http.StatusServiceUnavailable))
			})
		})

		Context("When reading light state contains leading/trailing whitespace", func() {
			BeforeEach(func() {
				fakeGpio.ReadReturns("\t0\n", nil)
				expectedLightState.StateKnown = true
				expectedLightState.LightOn = false
				expectedReturn, err = json.Marshal(expectedLightState)
Exemplo n.º 2
0
			Expect(fakeOSHelper.SleepArgsForCall(0)).To(Equal(door.SleepTime))

			Expect(fakeGpio.WriteHighCallCount()).To(Equal(1))
			Expect(fakeGpio.WriteLowCallCount()).To(Equal(1))

			actualHighPin := fakeGpio.WriteHighArgsForCall(0)
			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)