Testing uniqueness in scope in a Rails project using shoulda (but not rspec)

I have a custom class with a unique email, but it is attached to the tenant:

class User < ActiveRecord::Base validates :email, :uniqueness => {:scope => :tenant_id, :allow_blank => true} #... end 

I am trying to check it with:

 class UserTest < ActiveSupport::TestCase context "a user" do setup { @user = create :user } subject { @user } should validate_uniqueness_of(:email).scoped_to(:tenant_id) end end 

but the test fails with this message:

Expected errors for the inclusion of "have already been accepted" when the email is set to " joseph.allen_1@example.com ", received errors: ["the letter has already been received (" joseph.allen_1@example.com \ "))", "first_name cannot be empty (nil) "," last_name cannot be empty (nil) "] (with a different tenant_id value)

which raises a lot of questions. Why does the error message not match? It looks like the actual email address is included in the error message, but why is it included? When I generate an error from the user interface, it does not seem to be included:

enter image description here

In addition, at the end, he says that he is trying it with another tenant, who, if he was true, should not generate any errors (this does not happen when the application itself starts), but why is it expecting an error? He should only expect an error if he is the same tenant_id.

This is so confusing. Any ideas what is going on and / or how to verify this correctly?

+6
source share
2 answers

Just ran into this problem. We were able to resolve this by changing

 should validate_uniqueness_of(:email).scoped_to(:tenant_id) 

to

 should validate_uniqueness_of(:email_id).scoped_to(:tenant_id) 

I do not know if this was in the same situation, but, apparently, our problem was caused by the invested resources, which somehow threw a match.

+15
source

I found that scoped_to has unclear problems with specific tests. Whenever it occurs, it becomes necessary to write more detailed tests and manually verify the inability to create an object in the same area.

0
source

Source: https://habr.com/ru/post/910482/


All Articles