Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议:把单元测试写好 #51

Closed
iKuiki opened this issue Apr 26, 2019 · 2 comments
Closed

建议:把单元测试写好 #51

iKuiki opened this issue Apr 26, 2019 · 2 comments

Comments

@iKuiki
Copy link

iKuiki commented Apr 26, 2019

golang提供的单元测试很强大,也很实用
一方面,修改了代码,可以立刻跑一个单元测试,跑通再提交,以减少bug
另一方面,新手学习的时候test文件也是一个非常好的example
(我刚发现real分支有v2.0.0了,但是,没有文档一脸懵逼。。。如果这个时候有个test文件,就非常舒适了

附上一个简单的在mqant v1.8.0(master分支最新代码)下的一个login module的test片段:

func TestLogin(t *testing.T) {
	// 先创建一个服务器
	conf.LoadConfig("../bin/conf/server.json")
	app := mqant.CreateApp(true, conf.Conf) //只有是在调试模式下才会在控制台打印日志, 非调试模式下只在日志文件中输出日志
	app.AddRPCSerialize(common.RPCParamWegateResponseMarshalType, new(common.ResponseSerializer))
	//app.Route("Chat",ChatRoute)
	go app.Run(
		wgate.Module(), //这是默认网关模块,是必须的支持 TCP,websocket,MQTT协议
		login.Module(), //这是用户登录验证模块
	)
	time.Sleep(time.Second) // 小睡1秒等待mqant启动完成
	// -------------- 开始测试逻辑 --------------
	w := Work{}
	opts := w.GetDefaultOptions("tcp://127.0.0.1:3563")
	opts.SetConnectionLostHandler(func(client MQTT.Client, err error) {
		fmt.Println("ConnectionLost", err.Error())
	})
	opts.SetOnConnectHandler(func(client MQTT.Client) {
		fmt.Println("OnConnectHandler")
	})
	err := w.Connect(opts)
	if err != nil {
		panic(err)
	}
	// 未登陆情况下注销
	// 期望:返回RetCodeUnauthorized
	resp, err := w.Request("Login/HD_Logout", []byte(`{}`))
	if err != nil {
		t.Fatalf("Login/HD_Logout error: %+v", err)
	}
	if resp.Ret != common.RetCodeUnauthorized {
		t.Fatalf("not login yet, func(logout) shoud return RetCodeUnauthorized(401), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
	// 用户、密码为nil情况下登陆
	// 期望:返回RetCodeBadRequest
	resp, err = w.Request("Login/HD_Login", []byte(`{}`))
	if err != nil {
		t.Fatalf("Login/HD_Login error: %+v", err)
	}
	if resp.Ret != common.RetCodeBadRequest {
		t.Fatalf("login with empty info, shoud return RetCodeBadRequest(400), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
	// 正常登陆
	// 期望:返回RetCodeOK
	resp, err = w.Request("Login/HD_Login", []byte(`{"username":"abc","password":"test"}`))
	if err != nil {
		t.Fatalf("Login/HD_Login error: %+v", err)
	}
	if resp.Ret != common.RetCodeOK {
		t.Fatalf("login with empty info, shoud return RetCodeOK(200), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
	// 登陆后再登陆
	// 期望:返回RetCodeBadRequest
	resp, err = w.Request("Login/HD_Login", []byte(`{"username":"abc","password":"test"}`))
	if err != nil {
		t.Fatalf("Login/HD_Login error: %+v", err)
	}
	if resp.Ret != common.RetCodeBadRequest {
		t.Fatalf("login with empty info, shoud return RetCodeBadRequest(400), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
	// 正常注销
	// 期望:返回RetCodeOK
	resp, err = w.Request("Login/HD_Logout", []byte(`{}`))
	if err != nil {
		t.Fatalf("Login/HD_Logout error: %+v", err)
	}
	if resp.Ret != common.RetCodeOK {
		t.Fatalf("not login yet, func(logout) shoud return RetCodeOK(200), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
	// 已注销的情况下注销
	// 期望:返回RetCodeUnauthorized
	resp, err = w.Request("Login/HD_Logout", []byte(`{}`))
	if err != nil {
		t.Fatalf("Login/HD_Logout error: %+v", err)
	}
	if resp.Ret != common.RetCodeUnauthorized {
		t.Fatalf("not login yet, func(logout) shoud return RetCodeUnauthorized(401), but now is %v with Msg: %s", resp.Ret, resp.Msg)
	}
}
@iKuiki
Copy link
Author

iKuiki commented Apr 26, 2019

其实还可以写一个小方法来简化下面的测试,而不是大段的复制代码,写的匆忙,就一切从简了

@liangdas
Copy link
Owner

如果有兴趣的话可以加入mqant的维护开发工作(^_^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants