Implement mocking and update tests for createElection
This commit is contained in:
@ -4,7 +4,9 @@ import (
|
||||
"bytes"
|
||||
"code.dlmw.ch/dlmw/qv/internal"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
@ -15,6 +17,11 @@ func TestCreateElection(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(1, nil)
|
||||
|
||||
path := "/election"
|
||||
|
||||
tests := []struct {
|
||||
@ -180,3 +187,32 @@ func TestCreateElection(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateElection_ServerError(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(0, fmt.Errorf(""))
|
||||
|
||||
path := "/election"
|
||||
requestBody := api.CreateElectionRequest{
|
||||
Choices: []string{"宮本武蔵", "伊東一刀斎"},
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
AreVotersKnown: false,
|
||||
MaxVoters: 0,
|
||||
Name: "強",
|
||||
Tokens: 100,
|
||||
}
|
||||
requestBodyJson, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
code, _, _ := server.post(t, path, bytes.NewReader(requestBodyJson))
|
||||
|
||||
assert.Equal(t, 500, code)
|
||||
}
|
||||
|
Reference in New Issue
Block a user