I am newbie to ROR and working on a project which provides web services to a game. I have to write the test cases for that services.
How I can case against each model and controller case.
I wrote the following to create a new user in db
test "create" do
post(:create,
{
player:{
'player_name' => "usman",
'password' => 123,
'email' => 'ranasaani@gmail.com'
}
}
)
assert_select reponse.body
Controller code is
def create player = Player.create(params['player'])
if player.valid?
# if creation successful, log the player in:
player_session = PlayerSession.create(
player: player,
session_token: ActiveSupport::SecureRandom.urlsafe_base64
)
render json: {session_token: player_session.session_token}
else
render json: {error: "Player name already exists."}, status: :unprocessable_entity
end
end
But there is an error
SyntaxError: xxx/players_controller_test.rb:5: syntax error, unexpected ':'
post(:create, {'player':{'player_name' => "usman", 'password' => 123, 'email' => 'ranasaani@gmail.com'}})
Is there any guide, how to write the test cases ?
I'm not commenting on your testing methods but in most cases of your code examples, the syntax is more like JSON instead of ruby hashes. Use :player => { :player_name => "usman", ... }
. You can either use strings 'player'
or ruby symbols :player
as keys.
A more complete example with your render
call:
render :json => {:error => "Player name already exists."}, :status => :unprocessable_entity
Ruby will convert the hash to JSON itself. Or you can do it manually by calling .to_json
on it.