Ruby on rails RSpec水豚-请求测试。需要建议
有两件事我一点也不明白 1) 第一个示例:访问路径-失败和获取路径-通过,为什么? 拜访水豚并获得rspec助手,对吗Ruby on rails RSpec水豚-请求测试。需要建议,ruby-on-rails,rspec,capybara,integration-testing,Ruby On Rails,Rspec,Capybara,Integration Testing,有两件事我一点也不明白 1) 第一个示例:访问路径-失败和获取路径-通过,为什么? 拜访水豚并获得rspec助手,对吗 describe "in the Users controller" do describe "visiting the users page as non-signed" do before { get users_path } #before { visit users_path } it { expect(response.stat
describe "in the Users controller" do
describe "visiting the users page as non-signed" do
before { get users_path }
#before { visit users_path }
it { expect(response.status).to eql(302) }
it { expect(response).to redirect_to(new_user_session_path) }
end
describe "visiting the user[:id = 1] profile page as non-signed" do
before { get user_path(User.where(admin: true)) }
#before { visit user_path(User.where(admin: true)) }
it { expect(response.status).to eql(302) }
it { expect(response).to redirect_to(new_user_session_path) }
end
end
使用在此处获得一些路径->测试通过
但是使用访问此处的一些路径->
2) 第二个例子:
作为普通用户登录后,不应该有像管理员这样的菜单。
看起来用户和管理员之间没有区别
describe "as signed admin" do
let(:admin) { create(:admin) }
before do
log_in admin
end
it { should have_link("Users", href: users_path)}
it { should have_link("Orders", href: orders_path)}
it { should have_link("Current Menu", href: products_path)}
it { should_not have_link("Dashboard", href: new_order_path)}
end
describe "as signed user" do
let(:user) { create(:user) }
before do
log_in user
end
it { should have_link("Profile", href: user_path(user))}
it { should have_link("Dashboard", href: new_order_path)}
it { should_not have_link("Users", href: users_path)}
it { should_not have_link("Current Menu", href: products_path)}
end
include ApplicationHelper
def log_in(user)
visit root_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
def sign_up(user)
visit new_user_registration_path
fill_in 'Username', with: user.username
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
fill_in 'Password confirmation', with: user.password
click_button 'Sign up'
end
编辑1
我真的很喜欢……但我搞不懂……这有什么不对
let(:admin) { create(:admin) }
let(:user) { create(:user) }
factory :user do
sequence(:username) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "qwerty"
password_confirmation "qwerty"
factory :admin do
admin true
end
end
我的看法呢
- if user_signed_in?
%ul.nav.navbar-nav
%li
=link_to "Profile", current_user
- if !current_user.admin?
#if !current_user.try(:admin?)
%li
=link_to "Dashboard", new_order_path
- if !Order.get_order_for_user(current_user).nil?
%li
%a{:href => order_path(Order.get_order_for_user current_user)} Order
- else
%li
%a{:href => users_path} Users
%li
%a{:href => orders_path } Orders
%li
%a{:href => products_path } Current Menu
%ul.nav.navbar-nav.navbar-right
%li
= link_to "Sign Out", destroy_user_session_path, :method => :delete
it looks fine for me,but maybe i miss something...
编辑2
db/shcema的简短版本:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "avatar"
t.boolean "admin"
end
和我的用户模型:
before_save :set_default_role
private
# Set first user as Admin
def set_default_role
if User.count == 0
self.admin = true
end
end
编辑3-最后一个!;)强>
我在保存之前保存:设置默认角色
但在我的测试中,我做到了:
# User model -> before_save make first user admin.
let(:admin) { create(:user) }
let(:non_admin) { create(:user) }
before do
sign_up admin
log_in non_admin
end
我知道这可能并不理想,但它很有效,这对我的进步水平很好。但如果有人有英国石油公司的解决方案,我会注意到;) 1)当使用水豚时,您使用visit
和page
,当使用普通rails集成测试(RSpec请求规范是一个包装)时,您使用get
和response
-您不能将visit
和response
这样的测试混用在一起。此外,在Capybara中,大多数驱动程序不提供对请求-响应代码以及页面是否重定向等内容的访问,因为它旨在从用户角度进行测试,这意味着仅对浏览器中显示的内容进行回复
2) 从您的错误中可以看出,正常用户的行为与管理员用户的行为完全相同,这可能是由一些事情引起的,具体取决于您在页面上实际执行的操作。最简单的解释是,您对用户是否是管理员的条件检查没有正确实施,无论是在您的模型中,还是在您视图中的逻辑中。请以文本格式发布您的测试跟踪。
user\u path
routes to users show。例如,单数,User。其中(admin:true)
将返回一个集合,而不是单个用户。@j-dexx,它不满足,因为在db中总是只有一个用户。@mix fGt它确实重要。拥有一个用户对象和拥有一个包含一个用户的集合是有区别的。@j-dexx,同意!谢谢你的通知。修正)@mix fGt如果管理员和用户的行为仍然相同,请查看您对用户#admin的定义?也在用户/管理员工厂。我也会检查,thk。我在检查操作访问权限时遇到了几乎相同的问题-在操作之前。使用设计助手方法。但现在,你是怎么说的-检查模型或视图。鉴于我这样做:如果!当前_user.admin?然后显示常规菜单-其他->管理菜单。你怎么看?@mix fGt只要用户#admin就可以了?定义正确-并且:用户工厂没有意外创建管理员。请检查我的答案。@mix fGt假设您的“管理员”列为布尔值,并且默认值不是true,我认为这是正确的。另外,您是否定义了自己的用户管理员?方法还是使用rails自动提供的方法?