开发者

Right Rails way to write RPSECs to test URL routing?

I have a app with this kind of setup:

User (id)
Room (id, private (boolean)
RoomMember(user_id, room_id, banned (boolean)

These are the use cases I want to write tests for:

  1. If the room is not private the user can join and is redirect to the right url
  2. If the room is private, the user is redirect to the Enter Password Page
  3. If the user has been banned, they are redirect to '/'

What's开发者_如何学Python the right rails rspec way to test these user stories?

Thanks


The following should be possible (with your modification):

describe "User Permissions" do

  it "should allow users to join public rooms" do
    user = User.new
    room = Room.new( private: false )

    get "[your_action]"
    response.should render_template( "path/to/room/show/template" )
  end

  it "should honor privacy" do
    user = User.new
    room = Room.new( private: true )

    get "[your_action]"
    response.should redirect_to( action: '[enter_password_action]' )
  end

  it "shouldn't allow banned users to enter rooms" do
    user = User.new( banned: true )
    room = Room.new

    get "[your_action]"
    response.should redirect_to( "/" )
  end

end

More on writing Rspec Rails Controller tests can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜