mobilenig.TestNew   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nop 1
dl 0
loc 51
rs 8.7893
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package mobilenig
2
3
import (
4
	"net/http"
5
	"testing"
6
7
	"github.com/stretchr/testify/assert"
8
)
9
10
func TestNew(t *testing.T) {
11
	t.Run("default configuration is used when no option is set", func(t *testing.T) {
12
		// act
13
		client := New()
14
15
		// assert
16
		assert.NotEmpty(t, client.environment)
17
		assert.NotEmpty(t, client.common)
18
19
		assert.Empty(t, client.username)
20
		assert.Empty(t, client.apiKey)
21
22
		assert.NotNil(t, client.httpClient)
23
		assert.NotNil(t, client.Bills)
24
	})
25
26
	t.Run("single configuration value can be set using options", func(t *testing.T) {
27
		// Arrange
28
		env := TestEnvironment
29
30
		// Act
31
		client := New(WithEnvironment(env))
32
33
		// Assert
34
		assert.NotNil(t, client.environment)
35
		assert.Equal(t, env.String(), client.environment.String())
36
	})
37
38
	t.Run("multiple configuration values can be set using options", func(t *testing.T) {
39
		// Arrange
40
		env := TestEnvironment
41
		newHTTPClient := &http.Client{Timeout: 422}
42
43
		// Act
44
		client := New(WithEnvironment(env), WithHTTPClient(newHTTPClient))
45
46
		// Assert
47
		assert.NotEmpty(t, client.environment)
48
		assert.Equal(t, env.String(), client.environment.String())
49
50
		assert.NotNil(t, client.httpClient)
51
		assert.Equal(t, newHTTPClient.Timeout, client.httpClient.Timeout)
52
	})
53
54
	t.Run("it sets the Bills service correctly", func(t *testing.T) {
55
		// Arrange
56
		client := New()
57
58
		// Assert
59
		assert.NotNil(t, client.Bills)
60
		assert.Equal(t, client.environment.String(), client.Bills.client.environment.String())
61
	})
62
}
63