У меня был простой тест в моем record_controller_test.rb, который сначала прошел без проблем.
test "should create record" do
assert_difference('Record.count') do
post records_url, params: { record: { category_id: @record.category_id, user_id: @record.user_id } }
end
Я должен был убедиться, что каждая новая созданная запись уникальна, поэтому я добавил в модель следующее:
validates :user_id, :uniqueness => {:scope => :category_id}
Теперь тест не пройден. Как мне переписать тест, чтобы учесть новую проверку уникальности?
Обновление:
Вот код контроллера:
def new
@record = Record.new
@users = User.all
end
def create
@record = Record.new(record_params)
respond_to do |format|
if @record.save
format.html { redirect_to @record, notice: 'Record was successfully created.' }
format.json { render :show, status: :created, location: @record }
else
format.html { render :new }
format.json { render json: @record.errors, status: :unprocessable_entity }
end
end
end
Форма:
<div class="field">
<%= form.label :user_id %>
<%= form.collection_select :user_id, @users, :id, :name %>
</div>
<div class="field">
<%= form.label :category_id %>
<%= form.collection_select :category_id, @categories, :id, :category %>
</div>
<div class="actions">
<%= form.submit %>
</div>