2016-04-24 68 views
0

由於某些原因,我的控制器規範創建動作不起作用。我錯過了什麼?用於與工廠創建動作的rspec控制器規範

工廠

factory :profile do 
    first_name { "John" } 
    last_name { "Doe" } 
    job_title { Faker::Name.title } 
    company { "Faskyn" } 
    avatar { Faker::Avatar.image } 
    location { Faker::Address.city } 
    description { Faker::Lorem.sentence } 
    phone_number { Faker::PhoneNumber.cell_phone } 
    user 
end 

factory :product, class: Product do 
    name { Faker::Commerce.product_name } 
    company { Faker::Company.name } 
    website { 'https://example.com' } 
    oneliner { Faker::Lorem.sentence } 
    description { Faker::Lorem.paragraph } 
    user 
    trait :product_with_nested_attrs do 
    before(:create) do |product| 
     product.product_competitions << build(:product_competition, product: product) 
     product.product_usecases << build(:product_usecase, product: product) 
     product.product_features << build(:product_feature, product: product) 
     product.industries << build(:industry) 
    end 
    end 
end 

profiles_controller_spec

describe "POST create" do 
    before(:each) do 
    login_user 
    end 
    context "with valid attributes" do 

    it "saves the new profile in the db" do 
     expect{ post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user) }.to change(Profile, :count).by(1) 
    end 

    before(:each) do 
     post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user) 
    end 

    it { is_expected.to redirect_to add_socials_user_profile_path(@user) } 
    end 
end 

錯誤profiles_controller_spec

ProfilesController POST create with valid attributes saves the new profile in the db 
Failure/Error: expect{ post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user) }.to change(Profile, :count).by(1) 
    expected #count to have changed by 1, but was changed by 0 

products_controller_spec

context "POST create" do 
    context "with valid attributes" do 

    it "saves the new product in the db" do 
     product_attrs = attributes_for(:product, :product_with_nested_attrs, user: @user) 
     expect{ post :create, product: product_attrs }.to change{ Product.count }.by(1) 
    end 

    it { is_expected.to redirect_to product_path(Product.last) } 
    end 
end 

錯誤的products_controller_spec

ProductsController when user is logged in POST create with valid attributes saves the new product in the db 
Failure/Error: expect{ post :create, product: product_attrs }.to change{ Product.count }.by(1) 
    expected result to have changed by 1, but was changed by 0 

ProductsController when user is logged in POST create with valid attributes 
Failure/Error: it { is_expected.to redirect_to product_path(Product.last) } 

ActionController::UrlGenerationError: 
    No route matches {:action=>"show", :controller=>"products", :id=>nil} missing required keys: [:id] 
+0

你已經顯示了你的工廠,你的規格和失敗信息,但是你沒有顯示任何被測試的軟件,包括你的路由,你的控制器,除非你嘲笑ActiveRecord調用,楷模。通常情況下,這會讓人難以診斷問題。 –

回答

1

profiles_controller_spec,你有一個before_each這是創建用戶的第一個例子執行之前,因此額外的創建是不會碰的輪廓計數。請注意,在示例(it)之後放置before_each不會更改在給定級別的所有before_each塊都在包含的任何示例之前執行的事實。

至於products_controller_spec失敗,您需要顯示更多的代碼(例如相關變量和before包含塊中的塊,待測代碼等)。

+0

彼得,至於配置文件:我可以通過將'@ user.profile.destroy'放在'將新配置文件保存在數據庫'測試中的預期之前來解決問題。我想這不是最好的辦法。你能提供一些代碼,你會怎麼做?至於產品,我只是在這裏發佈了一個新問題:http://stackoverflow.com/questions/36827856/rspec-shoulda-setting-up-data你能看看嗎?我猜這個問題是相似的。我真的不知道如何正確設置數據。如果你知道一個好的資源,請讓我知道。 –