简体   繁体   English

如何在带有rspec的Rails控制器中测试关联?

[英]How do I test association in rails controller with rspec?

I have an article model which has many comments and the comment belongs to one article. 我有一个包含很多评论的文章模型,该评论属于一篇文章。 this is my create method for comments_controller.rb: 这是我的comment_controller.rb的创建方法:

def create
  @comment = Comment.new(comment_params)
  @comment.article_id = params[:article_id]

  @comment.save
  redirect_to article_path(@comment.article)
end

I want to know what's the best approach to test this action with rspec. 我想知道用rspec测试此操作的最佳方法是什么。 and I want to know testing methods for association in controller at all. 我想知道控制器中关联的测试方法。

thank you experts. 谢谢专家。

You can access your comment object within your tests using assigns method: 您可以使用assigns方法在测试中访问注释对象:

describe CommentsController, type: :controller
  let(:comment_params) {{ <correct params goes here>}}
  let(:article_id) { (1..100).sample }
  let(:create!) { post :create, comment: comment_params, article_id: article_id }

  it "creates new comment" do
    expect { create! }.to change { Comment.count }.by 1
  end

  it "assigns given comment to correct article"
    create!
    expect(assigns(:comment).article_id).to eq params[:article_id]
  end
end

The above is just a guideline, you will need to modify it depending on your exact requirements. 以上仅是一个准则,您将需要根据实际要求对其进行修改。

I suggest this codes. 我建议使用此代码。 This code is using FactoryGirl. 这段代码正在使用FactoryGirl。

factory_girl is a fixtures replacement with a straightforward definition syntax... https://github.com/thoughtbot/factory_girl Please add gem 'factory_girl_rails' to Gemfile . factory_girl是用简单的定义语法替换的灯具... https://github.com/thoughtbot/factory_girl请将Gemfile gem 'factory_girl_rails'添加到Gemfile

 def create
   @comment = Comment.new(comment_params)
   @comment.article_id = params[:article_id]

   if @comment.save
     redirect_to article_path(@comment.article)
   else
     redirect_to root_path, notice: "Comment successfully created" # or you want to redirect path
   end
 end

 describe "POST #create" do
   let(:article_id) { (1..100).sample }

   context 'when creation in' do
     it 'creates a new comment' do
       expect { post :create, comment: attributes_for(:comment), article_id: article_id }.to change {
        Comment.count
       }.from(0).to(1)
     end

     it 'returns same article_id' do
       post :create,  comment: attributes_for(:comment), article_id
       expect(assigns(:comment).article_id).to eq(article_id)
     end
   end

   context 'when successed in' do
     before { post :create, comment: attributes_for(:comment), article_id }

     it 'redirects article path' do
       expect(response).to redirect_to(Comment.last.article)
     end
   end

    context 'when unsuccessed in' do
     before { post :create, comment: attributes_for(:comment), article_id }

     it 'does not redirect article path' do
       expect(response).to redirect_to(root_path)
     end
   end
 end

uhh, I am not English native speaker. 呃,我不是英语为母语的人。 so If it 's sentence is not natural, please modify sentences. 所以如果it的句子是不自然的,请修改句子。 :-( :-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试控制器中的RSpec关联已设置? - How do I test RSpec association in controller was set? Rails 3.1 Rspec Rails 2-如何测试此控制器操作? - Rails 3.1 Rspec Rails 2 - How can I test this controller action? 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec? RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? 如何使用RSpec在我的Rails应用程序中测试JQuery-UI Sortable的控制器交互? - How do I test JQuery-UI Sortable's controller interaction in my Rails app using RSpec? Rspec rails:如何测试一次控制器操作是否正确地使用修改后的参数重定向到其自身? - Rspec rails: How do I test if a controller action redirects correctly to itself with modified params once? 使用Rspec,如何在Rails 3.0.11中测试我的控制器的JSON格式? - Using Rspec, how do I test the JSON format of my controller in Rails 3.0.11? Rails控制器Rspec测试 - Rails Controller Rspec Test Rails RSpec控制器测试 - Rails RSpec Controller Test 使用 Rspec 进行 Rails 控制器测试 - Rails controller test with Rspec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM