集成测试:在控制器中创建与新建
已编辑:遵循建议。请参阅问题末尾。
我有一个具有两个功能的控制器:
def new
if login_required
@discussion = Discussion.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @discussion }
end
end
end
def create
if login_required
@discussion = Discussion.new(params[:discussion])
@discussion.update_attribute("user_id",session[:userid])
respond_to do |format|
if @discussion.save
flash[:notice] = 'Discussion was successfully created.'
format.html { redirect_to(@discussion) }
format.xml { render :xml => @discussion, :status => :created, :location => @Discussion }
else
format.html { render :action => "new" }
format.xml { render :xml => @discussion.errors, :status => :unprocessable_entity }
end
end
end
end
现在,在我的集成测试文件中,我有:
test "test 1" do
post "/users/login",:user=> { :name => "bob", :password => "test_pass" }
post "/discussions/create", :discussion => { :title => "title 1", :body => "discussion body", :id => "101"} #Create 1
assert_response :success #Assert 1
get "/discussions/101"
assert_response :success #Assert 2
end
但是,我在断言 1 上得到 302
。
如果我将“Create 1”更改为: 发布“/discussions/new”,:discussion => {:标题=> "标题 1", :body => "讨论主体", :id => "101"}
我收到 404 错误。
1)发生了什么事?
2)我可以使用哪些工具/选项来自己解决这个问题?
谢谢
更新
遵循建议(来自 Ryan Bigg):
- post :create, { :discussion => { } }, { :user_id => users(:bob).id }
结果:
- Rack::Lint::LintError: env 变量 HTTP_USER_ID 具有非字符串值 1976283457
仍未解决。
edited: followed suggestions. See at end of question.
I have a controller with two functions:
def new
if login_required
@discussion = Discussion.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @discussion }
end
end
end
def create
if login_required
@discussion = Discussion.new(params[:discussion])
@discussion.update_attribute("user_id",session[:userid])
respond_to do |format|
if @discussion.save
flash[:notice] = 'Discussion was successfully created.'
format.html { redirect_to(@discussion) }
format.xml { render :xml => @discussion, :status => :created, :location => @Discussion }
else
format.html { render :action => "new" }
format.xml { render :xml => @discussion.errors, :status => :unprocessable_entity }
end
end
end
end
Now, in my integration test file I have:
test "test 1" do
post "/users/login",:user=> { :name => "bob", :password => "test_pass" }
post "/discussions/create", :discussion => { :title => "title 1", :body => "discussion body", :id => "101"} #Create 1
assert_response :success #Assert 1
get "/discussions/101"
assert_response :success #Assert 2
end
However, I get a 302
on Assert 1.
If I change "Create 1" to be:
post "/discussions/new", :discussion => { :title => "title 1", :body => "discussion body", :id => "101"}
I get a 404 error.
1) What is going on?
2) what tools/options are available to me so that I figure this out myself?
Thanks
update
Followed suggestion (from Ryan Bigg):
- post :create, { :discussion => { } }, { :user_id => users(:bob).id }
Result:
- Rack::Lint::LintError: env variable HTTP_USER_ID has non-string value 1976283457
Still unresolved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:
before_filter
。post :create, :discussion =>; { ... }
。如果您已正确设置控制器,则测试应该已经了解控制器。我相信命名约定是我头脑中的DiscussionsControllerTest
。我想为什么你在 Assert 1 上收到 302 是因为它实际上没有登录。尝试在对
post
的调用中传递凭据:post
的第二个参数是参数Hash
,第三个参数是会话Hash
。假设您已经加载了用户装置并且他们有一个名为 bob 的密钥,那么应该会找到该密钥并使用他来登录。我在 RSpec 测试中使用的另一种解决方案是定义一个
login_as< /code> 方法采用我应该登录的用户的登录名的单个参数。然后我在控制器或集成测试中执行的每个请求都以该用户身份登录:
我认为您应该能够在
test_helper.rb
文件中执行相同的操作,但是......我还没有' t 尝试过。 YMMV。A couple of things:
before_filter
.post :create, :discussion => { ... }
. The test should already know about the controller if you've set it up correctly. I believe the naming convention isDiscussionsControllerTest
off the top of my head.I think why you're getting the 302 on Assert 1 because it's not actually logging in. Try passing in the credentials in the call to
post
:The second argument of
post
is the paramsHash
and the third argument is the sessionHash
. Providing you have loaded user fixtures and they have a key called bob, this should find that and use him to log in.An alternative solution I use in my RSpec tests is to define a
login_as
method which takes a single argument of the login of the user whom I should log in as. Then every request I do in my controller or integration tests are logged in as that user:I think you should be able to do the same thing in your
test_helper.rb
file, but... I haven't tried it. YMMV.您的控制器中的一切似乎都工作正常。在您的测试中,您发送 POST 请求,而
new
操作不接受 POST 请求,而只接受 GET 请求。create
操作接受 POST,但它会在成功创建讨论后发送重定向。您可能应该将第一个
assert_response :success
替换为:Everything seems to be working correctly in your controller. In your tests you're sending a POST request and the
new
action doesn't accept POST requests, but only GET's. Thecreate
action accepts POST's, but it sends a redirect after successfully creating a discussion.You should probably replace the first
assert_response :success
with: