Issue in testing web app

blov · · 465 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>When I am writing my test case, I am using a views_test.go file in the views package, yet, the test cases says &#34;function not found&#34;, the function is a part of this package and it is unable to find it, when I try importing the views package unto itself (I know this is a cycling import, only then it works) it works file if I run <code>go test views/views_test.go</code> but fails at <code>go test ./views/</code></p> <pre><code> func TestWrongMethodSearchHandler(t *testing.T) { token := &#34;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN1cmFqIiwiZXhwIjoxNDg4NzA0MTg5fQ.eZKNxaOBJv0oRyFKZR5vF8u3xqKDvuGB_KAWsB2ElBI&#34; ts := httptest.NewServer(http.HandlerFunc(SearchHandler)) defer ts.Close() testCases := []struct { method string wantStatus int }{ { &#34;GET&#34;, http.StatusOK, }, } for _, tc := range testCases { req, err := http.NewRequest(tc.method, ts.URL, nil) req.Form = make(map[string][]string, 0) req.Form.Add(&#34;name&#34;, &#34;business&#34;) if err != nil { t.Fatal(err) } req.Header.Set(&#34;token&#34;, token) rr := httptest.NewRecorder() SearchHandler(rr, req) if status := rr.Code; status != tc.wantStatus { t.Errorf(&#34;handler returned wrong status code: got %v want %v&#34;, status, http.StatusOK) } } } </code></pre> <hr/>**评论:**<br/><br/>neopointer: <pre><p>It would be better if you post the code. But the only thing that I can think of right now is that you&#39;ve given a different package name in the test file. Like both files are in the same folder, but instead of both having package name <code>views</code>, the test file has package name <code>views_test</code>, and then from this package you can&#39;t access the unexported functions of the <code>views</code> package.</p></pre>thewhitetulip: <pre><p>That&#39;s what is strange, both files normal and test case have the same package name &#34;views&#34;. Still it throws that error! I have updated the question to contain the handler test case.</p> <p>After I get this to work, it says table not found for each SQL query, I am going nuts on this issue, can&#39;t seem to solve it.</p></pre>neopointer: <pre><p>That should be easy to track. It seems you are not connecting to the right database then...</p></pre>thewhitetulip: <pre><p>Now this is where I don&#39;t understand where things are headed! I am writing an app using sqlite3, I don&#39;t know which db the server is referring to!</p></pre>thewhitetulip: <pre><p>Can you show me an example unit test case about a web server? I didn&#39;t find a single document on the web which teaches how to test a webserver, I am writing a RESTful API.</p></pre>neopointer: <pre><p>Honestly I don&#39;t have much experience with testing in go. So I would suggest that you check how you are constructing your db connection to know where are you connecting to. It could be that are using an in-memory database connection with sqlite, so it will always be empty.</p></pre>thewhitetulip: <pre><p>I found out now that I am testing from inside the views directory, this it created a new sqlite database in my filepath, testing with that code now! I am still stumped as to why I have to import views inside the views package, it is strange.</p></pre>thewhitetulip: <pre><pre><code> views $ go test 2017/03/05 13:08:18 map[name:[business]] 2017/03/05 13:08:18 business 2017/03/05 13:08:18 searching for product name business 2017/03/05 13:08:18 map[name:[business]] PASS ok github.com/thewhitetulip/views 0.068s views $ go test views_test.go # command-line-arguments ./views_test.go:12: undefined: SearchHandler ./views_test.go:36: undefined: SearchHandler ./views_test.go:47: undefined: SearchHandler ./views_test.go:59: undefined: SearchHandler FAIL command-line-arguments [build failed] ``` </code></pre> <p>Okay, so I figured out the issue, I wasn&#39;t doing testing properly, I did <code>go test views_test.go</code> instead of <code>go test</code>, by doing <code>go test</code> it works fine!!</p></pre>neopointer: <pre><p>You can also use <code>go test ./...</code> from the root folder (it will test everything). Happy coding :)</p></pre>thewhitetulip: <pre><p>Thank you for your help!!</p></pre>tmornini: <pre><p>Shameless self promotion:</p> <p>Take a look at github.com/tmornini/http-spec</p> <p>I wrote it to test APIs via HTTP.</p> <p>It&#39;s fast, written in go, and is being actively used and developed.</p></pre>thewhitetulip: <pre><p>Thanks! For what it&#39;s worth, I don&#39;t see this as shameless self promotion, if not for self promotion how are you going to spread word about what you built? :-)</p> <p>Sure some might detest this, but let&#39;s not get swayed by a few such folks, let&#39;s do good work and spread the word about it!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

465 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传