email.TestEmailStrategy_Write   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 58
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 44
dl 0
loc 58
rs 8.3573
c 0
b 0
f 0
nop 1

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
////////////////////////////////////////////////////////////////////////////////
2
// Author:   Nikita Koryabkin
3
// Email:    [email protected]
4
// Telegram: https://t.me/Apologiz
5
////////////////////////////////////////////////////////////////////////////////
6
7
package email
8
9
import (
10
	"github.com/golang/mock/gomock"
11
	"github.com/mylockerteam/alog/mocks"
12
	"html/template"
13
	"runtime/debug"
14
	"testing"
15
16
	"github.com/mylockerteam/mailSender"
17
	"gopkg.in/gomail.v2"
18
)
19
20
func TestEmailStrategy_Write(t *testing.T) {
21
	mockCtrl := gomock.NewController(t)
22
	defer mockCtrl.Finish()
23
	type args struct {
24
		p []byte
25
	}
26
27
	from := "[email protected]"
28
	to := []string{"<[email protected]>"}
29
30
	msg := gomail.NewMessage()
31
	msg.SetHeader("From", "Example <[email protected]>")
32
	msg.SetHeader("Bcc", to...)
33
	msg.SetHeader("Subject", "Debug message")
34
35
	mockSendCloser := mocks.NewMockSendCloser(mockCtrl)
36
	mockSendCloser.EXPECT().Send(from, []string{"[email protected]"}, msg).Return(nil).AnyTimes()
37
38
	sender := mailSender.Create(&mailSender.Sender{
39
		Channel: make(chan mailSender.Message, 1),
40
		Closer:  mockSendCloser,
41
	})
42
43
	tpl, _ := template.New("test").Parse("<pre><code>{{ .Data }}</code></pre>")
44
45
	strategy := Get(sender, msg, tpl)
46
	stack := debug.Stack()
47
	tests := []struct {
48
		name    string
49
		fields  *Strategy
50
		args    args
51
		wantN   int
52
		wantErr bool
53
	}{
54
		{
55
			fields: strategy.(*Strategy),
56
			args: args{
57
				p: stack,
58
			},
59
			wantN:   len(stack),
60
			wantErr: false,
61
		},
62
	}
63
	for _, tt := range tests {
64
		t.Run(tt.name, func(t *testing.T) {
65
			s := &Strategy{
66
				Writer:   tt.fields.Writer,
67
				sender:   tt.fields.sender,
68
				Message:  tt.fields.Message,
69
				Template: tt.fields.Template,
70
			}
71
			gotN, err := s.Write(tt.args.p)
72
			if (err != nil) != tt.wantErr {
73
				t.Errorf("EmailStrategy.Write() error = %v, wantErr %v", err, tt.wantErr)
74
				return
75
			}
76
			if gotN != tt.wantN {
77
				t.Errorf("EmailWrite() = %v, want %v", gotN, tt.wantN)
78
			}
79
		})
80
	}
81
}
82