2016-04-01 25 views
2

Özellikle mesajları oluşturmak için, API için RSpec çalıştırıyorum İşte Hatalı istek nasıl giderilir (hata kodu 400)?

benim RSpec şunlardır:

(: güncelleştirme ve: Ben de üzerinde çalışıyorum. Oluşturun: yok fakat o iki ince çalışan ben sorun yaşıyorum)

.........F.F.... 

Failures: 

    1) Api::V1::PostsController authenticated and authorized users POST create returns http success 
    Failure/Error: expect(response).to have_http_status(:success) 
     expected the response to have a success status code (2xx) but it was 400 
    # ./spec/api/v1/controllers/posts_controller_spec.rb:78:in `block (4 levels) in <top (required)>' 
:
describe "POST create" do 
    before { post :create, topic_id: my_topic.id, post: {title: @new_post.title, body: @new_post.body} } 

    it "returns http success" do 
    expect(response).to have_http_status(:success) 
    end 

    it "returns json content type" do 
    expect(response.content_type).to eq 'application/json' 
    end 

    it "creates a topic with the correct attributes" do 
    hashed_json = JSON.parse(response.body) 
    expect(hashed_json["title"]).to eq(@new_post.title) 
    expect(hashed_json["body"]).to eq(@new_post.body) 
    end 
end 

Ve burada benim İşte

def create 
    post = Post.new(post_params) 

    if post.valid? 
     post.save! 
     render json: post.to_json, status: 201 
    else 
     render json: {error: "Post is invalid", status: 400}, status: 400 
    end 
    end 

ben almaya devam hata kodudur oluşturmaktır

Kodda neyin yanlış olduğundan emin değilim. Rotalar iyi çalışıyor. Testleri nasıl yapabilirim?

+1

Post.valid? 'False 'olduğu açıktır. Neden olduğunu bul! Belki de, denetleyici yönteminizin “else” bölümüne bir “p post.errors” ekleyerek. – spickermann

+0

Hata görmek için log/test.log dosyasını inceleyebilirsiniz. Alternatif olarak, hata ayıklamanıza yardımcı olması için sorunuza başka bağlantı da ekleyebilirsiniz. – Shishir

+0

@Shishir Soruyu hata ayıklama işlemi için nasıl bağlayabileceğimi paylaştığınızı paylaşır mısınız? Teşekkürler! – Iggy

cevap

1

Daha iyi bir çözüm için @spickermann, post'u @post numaralı telefonu değiştirin ve #create eyleminize ekleyin ve kodu aşağıya kendi özelliklerine ekleyin ve oradan çalışın. Bahse girerim, kontrolörünüzde @post.user = current_user gibi bir şey yapmanız gerekir.

it "@post is valid and have no errors" do 
    expect(assigns[:post]).to be_valid 
    expect(assigns[:post].errors).to be_empty 
end 
İlgili konular