【问题标题】:Testing Create Action in Rails Controller在 Rails 控制器中测试创建操作
【发布时间】:2015-09-06 13:42:57
【问题描述】:

在 post#create 上运行测试时,使用 RSpec 3 测试 Rails 4 应用程序会引发参数错误(0 为 2)。

我的控制器:

def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html  { redirect_to(@post,
                  :notice => 'Post was successfully created.') }
      format.json  { render :json => @post,
                  :status => :created, :location => @post }
    else
      format.html  { render :action => "new" }
      format.json  { render :json => @post.errors,
                  :status => :unprocessable_entity }
    end
  end
end

private

  def post_params
    params.require(:post).permit(:title, :body)
  end

我的路线:

resources :posts

我的测试:

describe 'POST posts#create' do
  it 'allows admin to create a new post' do
    sign_out user
    sign_in admin
    post :create, post: { title: 'Title', body: 'Body' }
    expect(response).to have_http_status(:success)
  end
end

问题出在控制器测试的post :create... 行。我不确定为什么它不能正常运行。我尝试过的解决方案:

1 post :create, post: title: 'Title', body: 'Body'

认为我的语法有问题。但是,这会引发错误。

2 post :create, { title: 'Title', body: 'Body' }

我认为帖子可能是隐含的,因为它是控制器的名称。没有骰子。

3 post :create, post:(post_params)

再次,我认为因为我已经设置了通常允许的参数,所以它们是必要的。毫不奇怪,该测试在其当前范围内没有 post_params。

4 post :create, post: { admin: { title: 'Title', body: 'Body' } }

我之前读过,您必须传入创建帖子项目的用户。不正确。

5 post :create=> { title: 'Title', body: 'Body' }

我认为发帖可能会隐式创建帖子,因为那是控制器。但是,它返回参数错误。

6 post :post=> { title: 'Title', body: 'Body' }

最后,我认为我必须发布一个新的帖子来提供参数,而不必使用创建操作。

编辑 - 7 post(:create, post: { title: 'Title', body: 'Body' })

在查看 API 文档 (http://api.rubyonrails.org/classes/ActionController/TestCase.html) 后,我认为这可行,但仍然是相同的参数错误。

8

describe 'POST admin#create' do
  it 'allows admin to create a new post' do
    sign_in admin
    expect{
      post :create, post: {title: 'Title', body: 'Body' }
    }.to change(Post, :count).by(1)
  end
end

我试图使用 expect 作为一个块,认为它会接受参数。没有。

妥协解决方案

describe 'POST admin#create' do
  it 'allows admin to create a new post' do
    sign_in admin
    expect{
      Post.create({title: 'Title', body: 'Body' })
    }.to change(Post, :count).by(1)
  end
end

我认为作为控制器名称的动作名称使 RSpec 失效。所以,我只是手动创建了一个帖子。可能不是推荐的解决方案。

这些都不起作用。我错过了什么?

错误输出

 Failure/Error: post(:create, post: { title: 'Title', body: 'Body' })
 ArgumentError:
   wrong number of arguments (2 for 0)
 # ./spec/controllers/posts_controller_spec.rb:101:in `block (3 levels) in <top (required)>'

我还删除了我所有的 before_filters 以查看是否是问题所在,但是,它给出了相同的错误。

【问题讨论】:

  • 你能显示实际的错误输出吗?
  • 我明白了。它来自请求内部。

标签: ruby-on-rails wordpress rspec


【解决方案1】:

您的语法看起来是正确的。它看起来像这样:

describe 'POST admin#create' do
  it 'allows admin to create a new post' do
    sign_out user
    sign_in admin
    post(:create, {post: {title: 'Title', body: 'Body'}})
    expect(response).to have_http_status(:success)
  end
end

...但是括号和外卷曲是可选的。

错误不是来自您的测试。错误发生在:create 的帖子中。在:create 请求期间,它来自控制器或模型内部来自。

【讨论】:

  • 我有一个半途而废的解决方案,就是打电话给Post.create 这不是很好,但这就是我想出的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多