我有一些控制器測試的問題。以下兩個失敗,但我真的不知道爲什麼。更新和銷燬正在工作。Ruby on Rails rspec控制器測試失敗,但不應該
我正在使用Rails 4.0和mongoid。
describe 'POST create' do
context 'with valid attributes' do
it 'creates a new restaurant' do
expect {
post :create, restaurant: FactoryGirl.attributes_for(:random_restaurant)
}.to change(Restaurant, :count).by(1)
end
it 'redirects to the new restaurant' do
post :create, restaurant: FactoryGirl.attributes_for(:random_restaurant)
response.should redirect_to Restaurant.last
end
end
我剛剛添加了從餐廳到地址的關係,更新了我的女工夾具。
這些都是我的燈具:
factory :random_restaurant, parent: :restaurant do |r|
r.name {Faker::Company.name}
r.description {Faker::Lorem.sentences}
after(:build) do |restaurant|
restaurant.addresses << FactoryGirl.build(:random_address)
end
end
factory :random_address, parent: :address do |a|
a.street {Faker::Address.street_address}
a.zip {Faker::Address.zip_code}
a.city {Faker::Address.city}
end
控制器後創建方法如下所示(仍然默認)
# POST /restaurants
# POST /restaurants.json
def create
@restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if @restaurant.save
format.html { redirect_to @restaurant, notice: 'Restaurant was successfully created.' }
format.json { render action: 'show', status: :created, location: @restaurant }
else
format.html { render action: 'new' }
format.json { render json: @restaurant.errors, status: :unprocessable_entity }
end
end
end
的錯誤是:
Failures: 1) RestaurantsController POST create with valid attributes creates a new restaurant
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/restaurants_controller_spec.rb:39:in `block (4 levels) in <top (required)>'
2) RestaurantsController POST create with valid attributes redirects to the new restaurant
Failure/Error: response.should redirect_to Restaurant.last
Expected response to be a <redirect>, but was <200>
# ./spec/controllers/restaurants_controller_spec.rb:46:in `block (4 levels) in <top (required)>'
Finished in 0.85251 seconds
19 examples, 2 failures
Failed examples:
rspec ./spec/controllers/restaurants_controller_spec.rb:38 # RestaurantsController POST create with valid attributes creates a new restaurant
rspec ./spec/controllers/restaurants_controller_spec.rb:44 # RestaurantsController POST create with valid attributes redirects to the new restaurant
這裏是更新正在工作的測試:
describe 'PUT update' do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
end
context 'with valid attributes' do
it 'locates the requested @restaurant' do
put :update, id: @restaurant, restaurant: FactoryGirl.attributes_for(:restaurant)
assigns(:restaurant).should eq(@restaurant)
end
it 'changes @restaurants attributes' do
put :update, id: @restaurant, restaurant: FactoryGirl.attributes_for(:restaurant)
@restaurant.reload
@restaurant.name.should eq('A Lodge')
@restaurant.description.should eq('A Bar')
end
it 'redirects to @restaurant' do
put :update, id: @restaurant, restaurant: FactoryGirl.attributes_for(:restaurant)
response.should redirect_to @restaurant
end
end
沒有人知道,爲什麼這會失敗,我該如何解決它? 非常感謝你
更新: 你的意思是這樣的嗎?這是從test.log中
Processing by RestaurantsController#create as HTML
Parameters: {"restaurant"=>{"name"=>"Schneider, Franecki and Tillman",
"description"=>["Quae labore quia officia soluta voluptatibus.", "Et error incidunt beatae laborum a libero officiis.", "Non excepturi dolor vel."],
"thumbnail"=>"some url", "banner"=>"some url"}}
**Unpermitted parameters: thumbnail, banner**
更新2
這些測試也工作:
describe Restaurant do
it 'has a valid factory' do
FactoryGirl.create(:random_restaurant).should be_valid
end
it 'has an invalid factory' do
FactoryGirl.build(:random_restaurant_with_invalid_address).should_not be_valid
end
it 'is invalid without a name' do
FactoryGirl.build(:random_restaurant, name: nil).should_not be_valid
end
it 'is invalid without a description' do
FactoryGirl.build(:random_restaurant, description: nil).should_not be_valid
end
it 'is invalid without an address' do
FactoryGirl.build(:random_restaurant_with_invalid_address).should_not be_valid
end
it 'creates an address when created' do
FactoryGirl.create(:random_restaurant)
end
end
更新3個
我的模型:
class Restaurant
include Mongoid::Document
has_many :addresses, dependent: :destroy
validates :name, presence: true
validates :description, presence: true
validates :addresses, presence: true
field :name, type: String
field :description, type: String
field :thumbnail, type: String
field :banner, type: String
end
class Address
include Mongoid::Document
belongs_to :restaurant
validates :street, presence: true
validates :zip, presence: true
validates :city, presence: true
field :street, type: String
field :zip, type: Integer
field :city, type: String
def full_address
zip_city = [zip, city].join ' '
[street, zip_city].join ', '
end
end
更新4
所以,我發現這一點: How is attr_accessible used in Rails 4?
和更新我的方法。但仍然不能工作:(
def restaurant_params
params.require(:restaurant).permit(:name, :description, :thumbnail, :banner, addresses_attributes: [ :street, :zip, :city ])
end
你能後的@ restaurant.errors.messages保存失敗後?這幾乎肯定會告訴你什麼是錯的。 –
作爲一個猜測,我懷疑這個問題是一個驗證地址,或類似的東西。如果FactoryGirl屬性不包含地址屬性,或者模型不支持嵌套設置屬性,則可能導致創建失敗並解釋更新工作的原因。 –
嗨,謝謝。但我在哪裏可以找到@ restaurant.errors.messages? – damir