Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

rspec - Hartl's Rails Tutorial Chapter 9 Exercise 6

Updating, showing, and deleting users, exercises

Is there a way to create an Rspec test for User controller actions such as "create" and "new?"

I'm not quite clear on the difference between the two actions "create" and "new" themselves either; could someone please be so kind as to elaborate?

After creating the test how would I go about implementing the redirect_to root_path? I think I am supposed to include the "new" and "create" actions in the before_filter signed_in section but this doesn't automatically redirect to the root.

I tried to get the tests to pass by modifying the users_controller.rb file as follows:

  def create
    if signed_in?
      redirect_to root_path
    else
      @user = User.new(params[:user])
      if @user.save
        sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
      else
        render 'new'
      end
    end
  end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did a before filter for that, seems to work well, for the test I did this:

on authentication_pages_spec.rb

describe "signin" do
  describe "authorization" do
    describe "for signed in users" do
      let(:user) { FactoryGirl.create(:user) }
      let(:new_user) { FactoryGirl.attributes_for(:user) }
      before { sign_in user }

      describe "using a 'new' action" do
        before { get new_user_path }
        specify { response.should redirect_to(root_path) }
      end

      describe "using a 'create' action" do
        before { post users_path new_user }
        specify { response.should redirect_to(root_path) }
      end         
    end
  end
end

Like @WillJones says, some people might have to add no_capybara: true to the before block

and on my users controller:

before_filter :signed_in_user_filter, only: [:new, :create]

def signed_in_user_filter
  redirect_to root_path, notice: "Already logged in" if signed_in?
end

For the difference between the new and create actions, it has to do with the REST architectural style, but basically, new is an action from users controller that responds to a GET request and is the one that's in charge of returning the view it responds to (in this case, a new user form). create on the other hand, is an action that responds to a POST request, it doesn't render anything (it can respond with javascript, but that's an advanced topic) and it's the one responsible for creating new users, like the action's name implies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...