Wednesday 15 April 2015

ruby on rails 3 - Rails3 validation okay in console fails in test_helper -


In my assumption there is a ((id) statement that works in the verification console but test_helper fails if I use that function I will change it with the array returned with that function, then all the test will be in the console and test_helper.

Here are the details:

I run Rail 3.2.13 at Kbuntu 12.04

Set up two models in a virgin project:

User - Rail G model user role_ ID: Integer

Role - Rail G model role role_name: string

app / model / user.rb

  class user & Lt; ActiveRecord :: Base attr_accessible: role_id #validates: role_id, included: {in: [1, 2, 3]} # Using the authentication result array Valid: role_id, include: {in: Role.pluck (: id )} # Using the Fence (: Id) function.   

The app / model / roll.rbb has generated.

Using the console, three roles have been added The id is [1, 2, 3].

Verification verified in console.

  U = User.New (role_id: 1) u.valid? = & Gt; True u.valid_out = value # outside the price range? = & Gt; False u.role_id = 2 value in the range u.valid? = & Gt; True,   

Therefore, it starts working as a validation.

  u.save   

has also worked.

Then, added a test:

test / unit / user_test.rb

  is required 'test-helper' class UserTest & lt ; ActiveSupport :: TESTSIS test "roll_id must be valid" @U = user NY (Roll_ID: 1) insisted (@U.Valid ?, "Invalid Role") and End   

Examination with Run Test

The exam has failed, though the role_id is correct according to the confirmation of the console results.

If I replace the verification with AP / Modal / UserRb with a comment above

  Valid: role_id, include: {in: [1, 2, 3 ]}   

and other validation, pass pass and console passes the same comment with the RSPC I repeated it several times, always have the same effect There were many interesting things on this site and proc, lambda, blocks, custom legalization, no success was found.

I think I have learned a lot, but now I am starting to get disappointed. Can anyone direct me to my mistake?

Thank you very much.

Run tests in a different environment and DB (Test environment / DB, not coincidentally).

You should be like some test data, roles.

It includes many options like things, DB sowing etc ..

You can create the same failure in the console by starting in test environments:

  RRES.NINV = test rail C    < / Html>

No comments:

Post a Comment