Exemple #1
0
func TestWeb(t *testing.T) {
	recorder := httptest.NewRecorder()
	handler := CreateHttpHandler()

	ggk := new(gogherkit.GoGherKit)

	ggk.AddMatcher("Given", "the web server is $status", func(params gogherkit.StepFuncParam) {
		// todo: figure out how to start/stop it at will. for now, just assume we're good.
	})

	ggk.AddMatcher("When", "I request the main page with name '$name'", func(params gogherkit.StepFuncParam) {
		name := params["name"]

		req, err := http.NewRequest("GET", fmt.Sprintf("/%s", name), nil)

		if err != nil {
			t.Error("Could not create HTTP request")
		}

		handler.ServeHTTP(recorder, req)
	})

	ggk.AddMatcher("Then", "the text that comes back contains '$text'", func(params gogherkit.StepFuncParam) {
		body := params["text"]
		if !strings.Contains(string(recorder.Body.Bytes()), body) {
			t.Error("Body does not contain the expected text")
		}
	})

	ggk.RunFeatureFile("web.feature")

}
Exemple #2
0
func TestHandler(t *testing.T) {
	ggk := new(gogherkit.GoGherKit)

	var loginAPI *LoginAPI
	var result bool

	ggk.Given("the login system with no data").Do(func(params gogherkit.StepFuncParam) {

		loginAPI = new(LoginAPI)
		loginAPI.Init()

	})

	ggk.When("I login with username $username and password $password").Do(func(params gogherkit.StepFuncParam) {
		result = loginAPI.AttemptLogin(params["username"], params["password"])
	})

	ggk.When("I register with username $username and password $password").Do(func(params gogherkit.StepFuncParam) {
		result = loginAPI.Register(params["username"], params["password"])
	})

	ggk.Then("the result should be $bool").Do(func(params gogherkit.StepFuncParam) {
		if (result && params["bool"] != "true") || (!result && params["bool"] != "false") {
			t.Error("Expected the result to be ", params["bool"], "actual", result)
		}
	})

	ggk.RunFeatureFile("login.feature")
}