集成测试:在控制器中创建与新建

发布于 2024-08-16 02:53:14 字数 1911 浏览 6 评论 0原文

已编辑:遵循建议。请参阅问题末尾。

我有一个具有两个功能的控制器:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殤城〤 2024-08-23 02:53:14

有几件事:

  1. 尝试将 login_required 移至 before_filter
  2. 对于您的测试,您应该执行 post :create, :discussion =>; { ... }。如果您已正确设置控制器,则测试应该已经了解控制器。我相信命名约定是我头脑中的DiscussionsControllerTest

我想为什么你在 Assert 1 上收到 302 是因为它实际上没有登录。尝试在对 post 的调用中传递凭据:

post :create, { :discussion => { } }, { :user_id => users(:bob).id }

post 的第二个参数是参数Hash,第三个参数是会话Hash。假设您已经加载了用户装置并且他们有一个名为 bob 的密钥,那么应该会找到该密钥并使用他来登录。

我在 RSpec 测试中使用的另一种解决方案是定义一个 login_as< /code> 方法采用我应该登录的用户的登录名的单个参数。然后我在控制器或集成测试中执行的每个请求都以该用户身份登录:

def login_as(name)
  request.session[:user] = users(name).id
end

我认为您应该能够在 test_helper.rb 文件中执行相同的操作,但是......我还没有' t 尝试过。 YMMV。

A couple of things:

  1. Try moving login_required to a before_filter.
  2. For your test you should be doing post :create, :discussion => { ... }. The test should already know about the controller if you've set it up correctly. I believe the naming convention is DiscussionsControllerTest 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:

post :create, { :discussion => { } }, { :user_id => users(:bob).id }

The second argument of post is the params Hash and the third argument is the session Hash. 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:

def login_as(name)
  request.session[:user] = users(name).id
end

I think you should be able to do the same thing in your test_helper.rb file, but... I haven't tried it. YMMV.

南风几经秋 2024-08-23 02:53:14

您的控制器中的一切似乎都工作正常。在您的测试中,您发送 POST 请求,而 new 操作不接受 POST 请求,而只接受 GET 请求。 create 操作接受 POST,但它会在成功创建讨论后发送重定向。

您可能应该将第一个 assert_response :success 替换为:

assert_redirected_to :controller => "discussions", :action => "show"`

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. The create action accepts POST's, but it sends a redirect after successfully creating a discussion.

You should probably replace the first assert_response :success with:

assert_redirected_to :controller => "discussions", :action => "show"`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文