Home > Blockchain >  Rspec object validation failing... unclear on `:foo` vs `"foo"`
Rspec object validation failing... unclear on `:foo` vs `"foo"`

Time:07-02

I've written a test and it seems so close but not quite right.

it "adds the correct permissions" do
  subject
  expect(policy.permissions).to have_key("users")
  expect(policy.permissions["users"]).to eq({ "all_users" => {
    can_view: false,
    can_manage: false,
  } })
end

Output:

expected: {"all_users"=>{:can_manage=>false, :can_view=>false}}
     got: {"all_users"=>{"can_manage"=>false, "can_view"=>false}}
     

Obviously I'm close but I'm not sure what the deal is in terms of the : notation versus the "got" output from the test itself. How do I get this to pass?

CodePudding user response:

String keys are not equal to symbol keys in Hash class. Here is the example code below:

hash = {}
hash[:a] = 1
hash['b'] = 2

hash[:a] # returns 1
hash['a'] # returns nil

hash[:b] # returns nil
hash['b'] #returns 2

so you should expect the result like this:

expect(policy.permissions["users"]).to eq({ 'all_users' => { 'can_view' => false, 'can_manage' => false, } })
  • Related