0

처음 게시하는 경우. Hartl의 RoR 튜토리얼을 통해 작업 해봤는데, 실패한 rspec 테스트에 갇혀 있습니다. 코드를 살펴보고 입력하는 대신 사이트에서 직접 복사하여 붙여 넣었지만 여전히 오류를 찾을 수 없습니다.RoR Hartl Tutorial : 섹션 11.1 - rspec "연산자가 존재하지 않습니다"오류가 발생했습니다.

도움 주셔서 감사합니다.

사양/모델/user_spec.rb

require 'spec_helper' 

describe User do 

    before { @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") } 

    subject { @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:email) } 
    it { should respond_to(:password_digest) } 
    it { should respond_to(:password) } 
    it { should respond_to(:password_confirmation) } 
    it { should respond_to(:remember_token) } 
    it { should respond_to(:authenticate) } 
    it { should respond_to(:admin) } 
    it { should respond_to(:microposts) } 
    it { should respond_to(:feed) } 
    it { should respond_to(:relationships) } 
    it { should respond_to(:follow!) } 
    it { should respond_to(:unfollow!) } 
    it { should respond_to(:followed_users) } 
    it { should respond_to(:reverse_relationships) } 
    it { should respond_to(:followers) } 


    it { should be_valid } 
    it { should_not be_admin } 

    describe "with admin attribute set to 'true'" do 
    before do 
     @user.save! 
     @user.toggle!(:admin) 
    end 

    it { should be_admin } 
    end 

    describe "when name is not present" do 
    before { @user.name = " " } 
    it { should_not be_valid } 
    end 

    describe "when email is not present" do 
    before { @user.email = " " } 
    it { should_not be_valid } 
    end 

    describe "when name is too long" do 
    before { @user.name = "a" * 51 } 
    it { should_not be_valid } 
    end 

    describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] [email protected]_baz.com [email protected]+baz.com [email protected]] 
     addresses.each do |invalid_address| 
      @user.email = invalid_address 
      expect(@user).not_to be_valid 
     end 
    end 
    end 

    describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
     addresses.each do |valid_address| 
      @user.email = valid_address 
      expect(@user).to be_valid 
     end 
    end 
    end 

    describe "when an email address is already taken" do 
    before do 
     user_with_same_email = @user.dup 
     user_with_same_email.email = @user.email.upcase 
     user_with_same_email.save 
    end 
    it { should_not be_valid } 
    end 

    describe "email address with mixed case" do 
    let(:mixed_case_email) { "[email protected]" } 

    it "should be saved as lower-case" do 
     @user.email = mixed_case_email 
     @user.save 
     expect(@user.reload.email).to eq mixed_case_email.downcase 
    end 
    end 

    describe "when password is not present" do 
    before do 
     @user = User.new(name: "Example User", email: "[email protected]", password: " ", password_confirmation: " ") 
    end 
    it { should_not be_valid } 
    end 

    describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
    end 

    describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_invalid } 
    end 

    describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by(email: @user.email) } 

    describe "with valid password" do 
     it { should eq found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not eq user_for_invalid_password } 
     specify { expect(user_for_invalid_password).to be_false } 
    end 
    end 

    describe "remember token" do 
    before { @user.save } 
    its(:remember_token) { should_not be_blank } 
    end 

    describe "micropost associations" do 

    before { @user.save } 
    let!(:older_micropost) do 
     FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago) 
    end 
    let!(:newer_micropost) do 
     FactoryGirl.create(:micropost, user: @user, created_at: 1.hour.ago) 
    end 

    it "should have the right microposts in the right order" do 
     expect(@user.microposts.to_a).to eq [newer_micropost, older_micropost] 
    end 

    it "should destroy associated microposts" do 
     microposts = @user.microposts.to_a 
     @user.destroy 
     expect(microposts).not_to be_empty 
     microposts.each do |micropost| 
     expect(Micropost.where(id: micropost.id)).to be_empty 
     end 
    end 

    describe "status" do 
     let(:unfollowed_post) do 
     FactoryGirl.create(:micropost, user: FactoryGirl.create(:user)) 
     end 

     its(:feed) { should include(newer_micropost) } 
     its(:feed) { should include(older_micropost) } 
     its(:feed) { should_not include(unfollowed_post) } 
    end 
    end 

    describe "following" do 
    let(:other_user) { FactoryGirl.create(:user) } 
    before do 
     @user.save 
     @user.follow!(other_user) 
    end 

    it { should be_following(other_user) } 
    its(:followed_users) { should include(other_user) } 

    describe "followed user" do 
     subject { other_user } 
     its(:followers) { should include(@user) } 
    end 

    describe "and unfollowing" do 
     before { @user.unfollow!(other_user) } 

     it { should_not be_following(other_user) } 
     its(:followed_users) { should_not include(other_user) } 
    end 
    end 
end 

응용 프로그램/모델/user.rb

class User < ActiveRecord::Base 
    has_many :microposts, dependent: :destroy 
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy 
    has_many :followed_users, through: :relationships, source: :followed 
    has_many :reverse_relationships, foreign_key: "followed_id", 
            class_name: "Relationship", 
            dependent: :destroy 
    has_many :followers, through: :reverse_relationships, source: :follower 
    has_secure_password 
    before_save { self.email = email.downcase } 
    before_create :create_remember_token 
    validates :name, presence: true, 
        length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i 
    validates :email, presence: true, 
         format: { with: VALID_EMAIL_REGEX }, 
         uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 } 

    def User.new_remember_token 
     SecureRandom.urlsafe_base64 
    end 

    def User.encrypt(token) 
     Digest::SHA1.hexdigest(token.to_s) 
    end 

    def feed 
     # This is preliminary. See "following users" for the full implementation. 
     Micropost.where("user_id = ?", id) 
    end 

    def following?(other_user) 
     relationships.find_by(followed_id: other_user.id) 
    end 

    def follow!(other_user) 
     relationships.create!(followed_id: other_user.id) 
    end 

    def unfollow!(other_user) 
     relationships.find_by(followed_id: other_user.id).destroy! 
    end 

    private 

     def create_remember_token 
      self.remember_token = User.encrypt(User.new_remember_token) 
     end 
end 

사양/모델/relationship_spec.rb

require 'spec_helper' 

describe Relationship do 

    let(:follower) { FactoryGirl.create(:user) } 
    let(:followed) { FactoryGirl.create(:user) } 
    let(:relationship) { follower.relationships.build(followed_id: followed.id) } 

    subject { relationship } 

    it { should be_valid } 

    describe "follower methods" do 
    it { should respond_to(:follower) } 
    it { should respond_to(:followed) } 
    its(:follower) { should eq follower } 
    its(:followed) { should eq followed } 
    end 

    describe "when followed id is not present" do 
    before { relationship.followed_id = nil } 
    it { should_not be_valid } 
    end 

    describe "when follower id is not present" do 
    before { relationship.follower_id = nil } 
    it { should_not be_valid } 
    end 
end 

RSpec에 스펙/PostgreSQL의에서 오류가 꽤 분명하다

rspec spec/ -e "User following" 
Run options: include {:full_description=>/User\ following/} 
..F.. 

Failures: 

    1) User following followed user followers 
    Failure/Error: its(:followers) { should include(@user) } 
    ActiveRecord::StatementInvalid: 
     PG::Error: ERROR: operator does not exist: integer = character varying 
     LINE 1: ...users" INNER JOIN "relationships" ON "users"."id" = "relatio... 
                    ^
     HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
     : SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = $1 AND "users"."id" = 15 LIMIT 1 
    # ./spec/models/user_spec.rb:177:in `block (4 levels) in <top (required)>' 

Finished in 0.3616 seconds 
5 examples, 1 failure 

Failed examples: 

rspec ./spec/models/user_spec.rb:177 # User following followed user followers 

Randomized with seed 7316 

답변

0

결과 :

없음 연산자는 주어진 이름과 인수 형식 (들)과 일치하지 않습니다. 명시 적 타입 캐스트를 추가해야 할 수도 있습니다.

과이 가입 조건을 가리키고하십시오 :string보다는 :integer과 PostgreSQL 뒤에 어떤 암시 적 형식 변환을하지 않을 것 같은 당신은 당신의 relationships 테이블의 follower_id 열을 만든

"users"."id" = "relationships"."follower_id" 

너의 뒤. Relationship 모델 (일명 relationships 표)에 follower_id의 정의를 수정하십시오.

+0

감사합니다. 그것은 트릭을했다. PostgreSQL 명령 줄 대신 레일즈에서이 작업을 수행하는 방법에 대한 [기타 게시물] (http://stackoverflow.com/questions/10690289/rails-gmaps4rails-gem-on-postgres)을 찾았습니다. 도움을 감사하십시오. –

관련 문제