홈>
저는 새로운 코더이므로 아마도 어리석은 일입니다. 아래 코드는 테스트 스위트에서 실패하지만 레일 콘솔에서 동일한 단계를 실행하면 예상 결과가 나타납니다.
aura_test.rb
require 'test_helper'
class AuraTest < ActiveSupport::TestCase
def setup
@champion = champions(:aura)
@aura = Aura.new description: "Lorem ipsum Aura blah", champion_id: @champion.id, stat_type_id: 1
end
test "should be valid" do
assert @aura.valid?
end
end
aura.rb
class Aura < ApplicationRecord
belongs_to :champion, optional: true
belongs_to :stat_type
belongs_to :affinity, optional: true
belongs_to :location_group, optional: true
validates :description, presence: true
validates :champion_id, presence: true
end
champions.rb
class Champion < ApplicationRecord
belongs_to :faction
belongs_to :affinity
belongs_to :rarity
belongs_to :champion_type
has_one :aura, dependent: :destroy
validates :name, presence: true
end
비품 :
champions.yml
name: AuraChamp
faction: one
affinity: one
rarity: one
champion_type: one
레일 콘솔에서 비슷한 코드를 실행할 때 :
>> @champion = Champion.first
Champion Load (0.1ms) SELECT "champions".* FROM "champions" ORDER BY "champions"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<Champion id: 1, name: "", faction_id: 1, affinity_id: 1, rarity_id: 1, champion_type_id: 1, created_at: "2019-07-19 23:50:18", updated_at: "2019-07-19 23:50:18">
>> @aura = Aura.new description: "Lorem ipsum Aura blah", champion_id: @champion.id, stat_type_id: 1
=> #<Aura id: nil, name: nil, description: "Lorem ipsum Aura blah", champion_id: 1, stat_type_id: 1, affinity_id: nil, location_group_id: nil, increase: nil, is_percent: nil, created_at: nil, updated_at: nil>
>> @aura.valid?
StatType Load (0.1ms) SELECT "stat_types".* FROM "stat_types" WHERE "stat_types"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> true
테스트 실행시 다음 결과가 나타납니다
FAIL["test_should_be_valid", AuraTest, 0.5133701380000275]
test_should_be_valid#AuraTest (0.51s)
Expected false to be truthy.
test/models/aura_test.rb:11:in `block in <class:AuraTest>'
- 답변 # 1
관련 자료
- ruby - Circle CI가 Rails 자격 증명을 찾지 못하지만 사양 테스트가 로컬에서 작동하는 이유는 무엇입니까?
- AngularJS 및 Codepen으로 Rails API 테스트
- Unsure how create method works - create 메소드가 어떻게 작동하는지 잘 모르겠습니다루비 온 레일
- ruby - 중복 입력으로 Rails 모델 고유성 테스트
- junit - Karate를 이용한 자동 테스트를 위해 MongoDB를 시드하는 올바른 방법
- ruby - heroku에서 webpack으로 rails에 반응하십시오css는 사용되지 않습니다 - 로컬 배포에서 작동
- amazon ec2 - ELIL for RAILS 서버 SSL을 사용하는 AWS EC2 인스턴스는 작동하지만 소셜 로그인에 실패
- Http 요청은 Android 9에서 실패하지만 Android의 이전 버전에서 작동합니다
- angular - 크롬 드라이버로 인해 각도기를 사용하지 않는 엔드 투 엔드 테스트
- 새로운 각도 프레임 워크 코드에 대한 자동화 된 단위 테스트에서 브라우저 호환성 테스트
- Facebook과 Rails 6 omniauth는 로컬에서 작동하지만 heroku에서는 작동하지 않습니다
- mongodb - Gradle 작업 명령이 실패하지만 수동으로 작동합니다
- assert_select와 assert_selector의 레일 테스트
- 자바 스크립트는 페이지 다시로드에서만 작동합니다 (레일, 터보 링크 설치)
- javascript - Ajax를 사용한 동적으로 자동 레일 저장 양식이 특정 지점에서 실패 함
- mysql - Mojave에서 MySQL2를 이용한 Rails 설치 실패
- selenium - 코딩이 필요없는 테스터를위한 자동 테스트
- Rails 설비의 레이블 연결이 "이름이 지정된 열 없음…"으로 실패합니다
- 원격 API 호출의 단위 테스트, JSON 오브젝트와 작동하는 방법
관련 질문
- Rails : Rails API 모드에서 protect_from_forgery를 구현하는 방법
- ruby on rails - 일대 다 및 다 대다 두 테이블 사이의 다중 참조
- ruby on rails - 중첩 된 속성 id 배열 구문 문제
- ruby on rails - 포트폴리오 앱에서 공개 지분 포지션 마감을 구현하려면 어떻게해야합니까?
- mysql - Mojave에서 MySQL2를 이용한 Rails 설치 실패
- Ruby on Rails의 뷰에서 선택 목록에 현재 사용자 레코드를 기록하는 방법
- ruby on rails - 하나의 컨트롤러 내에서 두 개의 서로 다른 테이블에 데이터를 삽입 할 수 있습니까?
- 레일은 배열에서 하위 배열을 제거합니다
- ruby on rails - 특정 고양이 가치를 가진 activereccord - 관계를 렌더링
- ruby on rails - JS보기에서 컨트롤러 인스턴스 변수가 렌더링되지 않음
모델에서 :
champion_id
에 대한 유효성 검사가 있습니다 이는 귀하가champion_id
에 값을 제공 할 때까지 귀하의 기록이 유효하지 않음을 의미합니다이제 비품을보고 :
id
가 없습니다 당신이@champion.id
로 여기에 액세스다음과 같이 조명기를 바꾸어보십시오 :
name: AuraChamp id: 1 faction: one affinity: one rarity: one champion_type: one